Jump to content

Help passing the selected file to an AppleScript


Recommended Posts

Posted
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  :rolleyes:
 

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

 
 
Posted

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
Posted (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):

 

aiFTw3N.png

 

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 by deanishe
Posted (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 by Jono

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...