Hey all, I am relatively new to AppleScript (and coding in general, for that matter), but I threw together a little function that works when tested outside of Hazel, but results in the error, "Error executing AppleScript (external script location): null", when I link to it in Hazel. I was trying to build a script that, when any document was put onto the desktop, would prompt the user for a "category" to put the file in. I was wondering if anyone could see a basic mistake I made or if theres something wrong with the way it runs in Hazel. Thanks!
- Code: Select all
global folderList
global processResult
global listSelection
global fileContainer
global fileCategory
on hazelProcessFile(theFile)
tell application "Finder"
set fileAddress to (POSIX path of theFile)
set fileContainer to container of (theFile) as string
set fileName to name of (theFile) as string
set folderList to name of every folder of folder fileContainer
set beginning of folderList to ("None")
set end of folderList to ("Other")
end tell
tell application "System Events"
activate
repeat
set processResult to (my pickCategory())
if processResult is not "Repeat" then
exit repeat
end if
end repeat
set fileCategory to name of processResult
end tell
return {hazelPassesScript:true, hazelStop:false, hazelOutputAttributes:{fileCategory}}
end hazelProcessFile
on pickCategory()
tell application "System Events"
activate
set listSelection to (choose from list folderList without multiple selections allowed) as string
if listSelection is "None" then
return my noneSelected()
else if listSelection is "Other" then
return my otherSelected()
else
return listSelection
end if
end tell
end pickCategory
on noneSelected()
tell application "System Events"
activate
set confirmButton to "Are you sure you want to skip categorizing?"
try
set confirmDialog to (display dialog confirmButton buttons {"Cancel", "Confirm"} default button "Confirm" cancel button "Cancel" with icon caution)
set confirmResponse to button returned of confirmDialog
on error
set confirmResponse to "Cancel"
end try
end tell
if confirmResponse is not "Cancel" then
return "None"
else
return "Repeat"
end if
end noneSelected
on otherSelected()
tell application "System Events"
activate
set otherFolder to the text returned of (display dialog "Enter a name for new category:" default answer "")
end tell
tell application "Finder"
set newFolder to {name:otherFolder}
make new folder at fileContainer with properties {name:otherFolder}
return newFolder
end tell
end otherSelected