How to pass a full file path and it's name to a shell script?

Hello,

I want to create an AS that displays a Leopards Quick Look preview (that sticks on top of all windows) of a selected file (tho I’d prefer a drag’n’dropped file onto the Script).

tell application "Finder"
	set theSelection to (selection of application "Finder")
	set theFile to POSIX path of theSelection
	tell application "Terminal"
		activate
		do script "qlmanage -p " & quoted form of theFile
	end tell
end tell

So far, I encounter a problem of interpreting the file path which seems to be split in ‘classes’ or something (yes, I’m not AS savvy):

I’m stuck with this. Any pointers?
Thanks, Patrick

Hi Patrick,

I am sure you are interested in Apple’s Quick Look Droplet. The script can be opened in Script Editor, so you can easily study the source code.

Generally a shell command expects quoted Posix paths, which you can produce like this:


tell application "Finder"
	set finderitems to selection
end tell

repeat with finderitem in finderitems
	set itempath to quoted form of (POSIX path of (finderitem as Unicode text))
	set command to "open " & itempath
	set output to do shell script command
end repeat

Way cool, thanks for your help Martin!

I mostly am going to use this particular script to review audio files, since in 10.5 finder windows only provide start/stop buttons for this task, Quick Look has a couple more options :wink: . I’ve had a glance at the Quick Look Droplet linked in your post, the Droplet just does what I need. But I’d like to have much more Droplets running shell scripts and your example showed me how to do these. For the record:

tell application "Finder"
	set finderitems to selection
end tell

repeat with finderitem in finderitems
	set itempath to quoted form of (POSIX path of (finderitem as Unicode text))
	set command to "qlmanage -p " & itempath
	set output to do shell script command
end repeat

. runs fine as a script, but not in Automator or saved as a Script application. This is what I found in the Droplet you linked to:

property application_name : "Quick Look Droplet"

on run
	-- get path to this applet
	set this_application_file to the path to me
	tell application "System Events"
		set this_version to (the short version of this_application_file) as string
	end tell
	set the dialog_title to application_name & space & "v" & this_version
	
	activate
	display dialog "Drag one or more disk items onto this droplet to display them in a Quick Look window." buttons {"OK"} default button 1 with icon 1 with title dialog_title
end run

on open these_items
	set these_paths to ""
	repeat with i from 1 to the count of these_items
		set these_paths to these_paths & space & (the quoted form of the POSIX path of (item i of these_items))
	end repeat
	
	do shell script "qlmanage -p " & these_paths & " >& /dev/null"
end open

I only have a vague idea about the differences. The latter runs fine when exported as a Script.app but again not in Automator. I don’t care much for Automator but it would be better to know why exactly the latter script runs when being exported as an application.

The latter script has an “on open” handler. That handler makes the script a droplet i.e. when you drop files on it that handler is run and conversely when you double-click the app the “on run” handler is run. So that’s why you save it as an application… so you can drop files on it. That also explains why it doesn’t work in automator. But it would work there if you removed the code from the “on open” handler.

Cheers Hank!