Jono Posted February 2, 2014 Posted February 2, 2014 I have a script that checks if an Automator workflow or AppleScript is selected and then runs it. It works fine with the current file selected in Finder, but I'm having trouble getting it to work with the selected file passed from Alfred. I'm guessing (hoping) it's something simple. (I realise I could set it to run AppleScripts in Alfred's preferences, but a lot of the time want top open them, not run them.) Here's the script that works in Finder property automatorExtension : "workflow" property applescriptExtension : "scpt" tell application "Finder" set theFile to item 1 of (get selection) -- If the file is an Automator workflow if (the name extension of theFile is in the automatorExtension) then set workflowPath to theFile as text set quotedWorkflowPath to quoted form of (POSIX path of workflowPath) set command to "/usr/bin/automator " & quotedWorkflowPath set output to do shell script command else -- If the file is an AppleScript if (the name extension of theFile is in the applescriptExtension) then set theScript to theFile as alias try run script (theScript) on error _error display dialog _error end try else -- If not an Automation workflow or an AppleScript display dialog ¬ ¬ "The selected file is not an Automation workflow or an AppleScript" buttons ¬ {"Cancel", "OK"} cancel button "Cancel" default button "OK" giving up after 5 end if end if end tell And here's my botched adaptation for Alfred. I can never remember if I'm supposed to use 'Run NSAppleScript' or 'Run Script' and set it to osascript property automatorExtension : "workflow" property applescriptExtension : "scpt" on alfred_script(q) set theFile to q -- If the file is an Automator workflow if (the name extension of theFile is in the automatorExtension) then set workflowPath to theFile as text set quotedWorkflowPath to quoted form of (POSIX path of workflowPath) set command to "/usr/bin/automator " & quotedWorkflowPath set output to do shell script command else -- If the file is an AppleScript if (the name extension of theFile is in the applescriptExtension) then set theScript to theFile as alias try run script (theScript) on error _error display dialog _error end try else -- If not an Automation workflow or an AppleScript display dialog ¬ ¬ "The selected file is not an Automation workflow or an AppleScript" buttons ¬ {"Cancel", "OK"} cancel button "Cancel" default button "OK" giving up after 5 end if end if end alfred_script
deanishe Posted February 2, 2014 Posted February 2, 2014 It's probably better to use Run Script + osascript as RunNSAppleScript will block Alfred till it's done. The reason your Alfred script isn't working is because your Finder script is passing a File/Folder object (alias?) to the script, but Alfred passes a text object (the file's POSIX path). You probably need to start your Alfred script with something like: set theFile to POSIX file q as alias
Jono Posted February 3, 2014 Author Posted February 3, 2014 Thanks, but that doesn't work either (at least not for me). Here's the workflow if you want to look at it or try it yourself http://d.pr/f/Tv7f
deanishe Posted February 3, 2014 Posted February 3, 2014 (edited) You don't use on alfred_script(q) with /usr/bin/osascript, but "{query}", as with other script types. This works (with caveats—see below): property automatorExtension : "workflow" property applescriptExtension : "scpt" set filePath to "{query}" set theFile to POSIX file filePath as alias tell application "Finder" -- If the file is an Automator workflow if (the name extension of theFile is in the automatorExtension) then set workflowPath to theFile as text set quotedWorkflowPath to quoted form of (POSIX path of workflowPath) set command to "/usr/bin/automator " & quotedWorkflowPath set output to do shell script command else -- If the file is an AppleScript if (the name extension of theFile is in the applescriptExtension) then set theScript to theFile as alias try run script (theScript) on error _error display dialog _error end try else -- If not an Automation workflow or an AppleScript display dialog ¬ ¬ "The selected file is not an Automation workflow or an AppleScript" buttons ¬ {"Cancel", "OK"} cancel button "Cancel" default button "OK" giving up after 5 end if end if end tell But you have to set the escaping properly (yours was wrong): Finally, it's worth noting that not all AppleScripts end with .scpt. I save mine in plain text format, so they have the extension .applescript. Also, you don't need that section at the end showing an error if the file isn't an Automator workflow or an AppleScript: the file filter ensures that only files of that type get passed to the action. Edited February 3, 2014 by deanishe Jono 1
Jono Posted February 3, 2014 Author Posted February 3, 2014 (edited) Thanks a lot for the help! You don't use on alfred_script(q) with /usr/bin/osascript, but "{query}", as with other script types. Ah, I didn't realise that. Finally, it's worth noting that not all AppleScripts end with .scpt. I save mine in plain text format, so they have the extension .applescript. Good point, I'll add that extension in there too. Also, you don't need that section at the end showing an error if the file isn't an Automator workflow or an AppleScript: the file filter ensures that only files of that type get passed to the action. Yea, I'll remove that. Edited February 3, 2014 by Jono
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now