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.


Thanks in advance!