Prompt user for file name and resume matching rules

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

I just upgraded—not that I'm trying out a new feature, just sayin.

A few of my Hazel rules for my Downloads folder is to watch and look for any file without an extension. This happens a lot when the files being saved from my browser are being generated by PHP or some other script, and end up as just 'png' or 'woff' and so on, without the quotes. My Hazel rules give them a name based on the domain they originate from and then add the extension, after a period of course, so an example file might be 'iconfont.ttf' — But I'd like to have Hazel prompt me for the filename.

I did some searching for "prompt user for filename" and other queries with similar wording, because I''ve never used workflows of Applescript with Hazel before. I have use them separately though. The matching search results either didn't match, or they only explained how to pass files/folders off to Automator AFTER Hazel is done with it's rules, not how to perform part of a rule: like adding the extension TTF, then prompt the user for a filename before the extension, before RETURNING to Hazel to rename the file.

I realise you probably get a lot of these newcomer questions, and I've seen that many are told to find their own answers. But this is really over my head and I stumped. Prompting the users for the filename and applying it seems like it would be simple enough for most other people but I tried and I really can’t do this alone. I hope my purchase and now my upgrade covers that much! Thanks in advance!

I'm not a programmer at all.
Ruby
 
Posts: 7
Joined: Tue Jul 26, 2016 1:26 am

Doing a search brings this up: viewtopic.php?f=4&t=1445&hilit=prompt#p5814

I suggest starting with that.
Mr_Noodle
Site Admin
 
Posts: 8798
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Doing a search brings this up: http://www.noodlesoft.com/forums/viewto ... ompt#p5814
I suggest starting with that.


Yes I had found that already but as I said I have no experience, so even if the code used in that thread could have been tailored to my needs, I am still confused about several aspects and languages of scripting. I find it confusing how many users don't say what kind of script the code they are using is. I'm sure most people can just look at it and immediately know if it is a bash but I don't. I'm also confused about how one can script entirely in Hazel, or start in Hazel, move to Automator, Script Editor, or load an external shell script, before returning to Hazel once the script has run. And even with those options there are more choices, like whether to writer Applescript in Automator. I don't know if these are personal preferences, or if there are intrinsic benefits to doing it scripting in a certain app.

Okay, enough with the list of things that confuse me in general, let me try to describe my Hazel rule, what I'd like it to do, what I've tried, and then perhaps someone can help me take the next step forward.

I've created a rule to watch my downloads folder for files that are saved without names or extensions. This happens a lot with Github, or when I am taking eLearning classes, and files will be generated from PHP or converted from Base64 and I get a font named "ttf" for example. The first part of the rule is to make the ttf into a readable font by adding the ".ttf" extension. That is easy enough, but something needs to go before the extension. That’s why I want a prompt to appear, and ask me (or whoever the user is) for a filename. But because the filename has not yet been defined, the adding of the ".ttf" extension should really be the second step not the first.

So I found this Applescript for a file-name prompt on the Apple Developer website. It looks to be more up to date than that other post which was written 4 years ago:

Code: Select all
set theResponse to display dialog "Please enter the font name:" default answer ""
set theNote to text returned of theResponse

set theNewFilePath to choose file name with prompt "Save the document as:"

writeTextToFile(theNote, theNewFilePath, true)

on writeTextToFile(theText, theFile, overwriteExistingContent)
    try

        -- Convert file to a string
        set theFile to theFile as string

        -- Open file for writing
        set theOpenedFile to open for access file theFile with write permission

        -- Clear file if content should be overwritten
        if overwriteExistingContent is true then set eof of theOpenedFile to 0

        -- Write new content to file
        write theText to theOpenedFile starting at eof

        -- Close file
        close access theOpenedFile

        -- Return a boolean indicating that writing was successful
        return true

        -- Handle a write error
    on error

        -- Close file
        try
            close access file theFile
        end try

        -- Return a boolean indicating that writing failed
        return false
    end try
end writeTextToFile


I have this as an embedded Applescript in the Hazel workflow. When I pasted the code in, it says to use
theFile to refer to the file being processed and inputAttributes to access imported attributes.

I thought that meant I could have Hazel rename the file in the next step of the workflow, by choosing the "rename" function and entering the variable "theFile" followed by ".ttf" (without quotes), but that’s not working. As fat as accessing imported attributes, I'm guessing that doesn't apply to my needs.

So to review please take a look at the screenshots of my rule, and please someone tell me how I can make it work, and if I'm that far off.

Image
Image

Thanks in advance!
Ruby
 
Posts: 7
Joined: Tue Jul 26, 2016 1:26 am

There are quite a few issues here. First off, don't expect to use random AppleScripts out there in Hazel as-is. The script I linked to can be used as it was specifically written with Hazel in mind. The missing piece there is in Hazel's UI, you need to specify an export attribute, "NewFilename". After you do that, you can use the "NewFilename" attribute in the subsequent actions.
Mr_Noodle
Site Admin
 
Posts: 8798
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:The script I linked to can be used as it was specifically written with Hazel in mind.

Okay, I will take another look at the script you linked to again, hopefully it will make a little more sense having spent some time trying to learn AppleScript since then.

Mr_Noodle wrote:The missing piece there is in Hazel's UI, you need to specify an export attribute, "NewFilename". After you do that, you can use the "NewFilename" attribute in the subsequent actions.

What is an "export attribute"? Do you mean this code in the script you linked to?

Code: Select all
if (all) of the following...
    Name MATCHES Screen Shot (...)

then
    Run AppleScript (exporting NewFilename)
    Move, or whatever else.


Obviously I'll change "MATCHES Screen Shot" to "MATCHES ttf"... but anyway this is an example of what I was talking about when I said people don't always specify what kind of script their code is in. Is that AppleScript?
Ruby
 
Posts: 7
Joined: Tue Jul 26, 2016 1:26 am

Yes, it's AppleScript. Search the help for "AppleScript". The article there outlines most of it, including how to deal with custom attributes exported by the script.
Mr_Noodle
Site Admin
 
Posts: 8798
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Yes, it's AppleScript. Search the help for "AppleScript". The article there outlines most of it, including how to deal with custom attributes exported by the script.


I'm sorry. You must really think I'm an idiot, but where is "the help"? I don't see any part of the forums or the noodlesoft site called "the help." i see faq's but that’s pretty much just rules on forum etiquette.
Ruby
 
Posts: 7
Joined: Tue Jul 26, 2016 1:26 am

In Hazel itself. You can access it either via the Help menu or the ? buttons in the UI.
Mr_Noodle
Site Admin
 
Posts: 8798
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:In Hazel itself. You can access it either via the Help menu or the ? buttons in the UI.


Okay, so should I separate the process into two Applescripts (like the other user) and just have the first return a pass or fail to Hazel, telling it whether or not to stop or to continue and run the Applescript that prompts user for filename?

I don't know how resource-hungry my finalised rule will be, but the help documents suggest that separating the two Applescripts is a good idea because "since rule conditions are evaluated many times, your script should run fairly quickly otherwise."
Ruby
 
Posts: 7
Joined: Tue Jul 26, 2016 1:26 am

I'm not sure where you are getting the idea that you need to have two scripts. Can you elaborate on that?
Mr_Noodle
Site Admin
 
Posts: 8798
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support