Help converting my script to Droplet

Hi all, I’m new to Applescripting, and thanks to this site so far the learning curve has been reasonably painless.

Bascialy I have found myself stuck trying to convert my script into a droplet. The guts of the script is that it reads in a folder containing an image sequence of type abc.0001.ext and returns …/path…/abc.#.ext for command line rendering.

If you guys had any suggests for optimising this and helping my with syntax for a droplet I’d be a happy man. :wink:

Thanks!


on run
	tell application "Finder"
		set the_path to choose folder
		set range to (number of files in folder the_path)
		set first_file_path to item 1 in folder the_path
		set the first_file_name to (the name of the first_file_path) as string
	end tell
	
	--set scaling factor
	set question_scale to "enter scaling percentage (0-100%)"
	set ans_scale to text returned of (display dialog question_scale default answer "100")
	
	--strip off suffix and number padding
	set item_length to length of first_file_name
	set the base_name to (characters 1 thru (item_length - 9) of first_file_name) as string --removes padding & suffix from end of filename
	set the extension to (characters (item_length - 3) thru (item_length) of first_file_name) as string
	
	--set file_out name for command line
	set the_path to POSIX path of the_path
	set file_out to (the_path & base_name & ".#" & extension)
	
	--run shake script
	tell application "Terminal"
		activate
		set shakescript to "shake " & file_out & " -z " & (ans_scale * 0.01) & " -t 1-" & range
		set windowCount to (count of the windows)
		if windowCount is greater than 0 then
			repeat with w from 1 to windowCount
				if window w is busy then
					set frontmost of window w to false
				else
					do script shakescript in window w
					set frontmost of window w to true
					return
				end if
			end repeat
		end if
		tell window 1
			do script shakescript
			set frontmost to true
		end tell
	end tell
end run

Hi john,

try this

on run
	set Folder_path to choose folder
	process_folder(Folder_path)
end run

on open these_items
	if folder of (info for item 1 of these_items) then process_folder(item 1 of these_items)
end open

on process_folder(Folder_path)
	tell application "Finder" to tell (get items of the_path) to set {range, first_file_path, first_file_name, extension} to {count it, item 1, name of item 1, name extension of item 1}
	
	--set scaling factor
	set ans_scale to text returned of (display dialog "enter scaling percentage (0-100%)" default answer "100")
	
	--strip off suffix and number padding 
	set the base_name to text 1 thru -9 of first_file_name --removes padding & suffix from end of filename
	
	--set file_out name for command line
	set file_out to quoted form of (POSIX path of the_path & base_name & ".#" & extension)
	
	--run shake script
	tell application "Terminal"
		activate
		set shakescript to "shake " & file_out & " -z " & (ans_scale * 0.01) & " -t 1-" & range
		set windowCount to (count of the windows)
		if windowCount is greater than 0 then
			repeat with w from 1 to windowCount
				if window w is busy then
					set frontmost of window w to false
				else
					do script shakescript in window w
					set frontmost of window w to true
					return
				end if
			end repeat
		end if
		tell window 1
			do script shakescript
			set frontmost to true
		end tell
	end tell
end process_folder

PS: I have no idea of Shake, but isn’t it possible to run the shakescript directly?

	--run shake script
	do shell script "shake " & file_out & " -z " & (ans_scale * 0.01) & " -t 1-" & range

Awesome! Thanks Stefan! :smiley:

It’s always cool to see how efficiently a script can be written when someoen knwos what they’re doing.

Good point on the shell command; it’s definitely a lot cleaner, the only reason for running Terminal is to give me a log of any errors, but since everything seems to be working perfectly now so I’ll swap in the shell line.

One small thing was I had to match the_path to Folder_path variables, but then it was perfect.

If I’m ever in Sankt Gallen I’ll buy you a beer!

John

Here is a little extension to log the errors in a log file which can be read with console.app.
I skipped the beginning of the handler"

property logName : "shake.log"
property logFolder : ((path to current user folder as Unicode text) & "Library:Logs:")

on run
	set Folder_path to choose folder
	process_folder(Folder_path)
end run

on open these_items
	if folder of (info for item 1 of these_items) then process_folder(item 1 of these_items)
end open

on process_folder(Folder_path)
	...
	...
	...
	--run shake script
	try
		do shell script "shake " & file_out & " -z " & (ans_scale * 0.01) & " -t 1-" & range
	on error e
		set logMessage to (((current date) as string) & ": " & (base_name & ".#" & extension) & " - " & e & return)
		write_log from logMessage into (logFolder & logName)
	end try
end process_folder

on write_log from theMessage into logFile
	set logFile to logFolder & logName
	set the logStream to open for access file logFile with write permission
	set logFileEof to get eof of the logStream
	write theMessage & return to logStream starting at eof as «class utf8»
	close access logStream
end write_log

Brilliant! Thanks again Stefan.

StefanK,

this looks really cool but could you explain to me how it works because I don’t even see where “the_path” is defined. I just don’t understand how its working. :confused:

I think it may have been a typ. Just change var the_path to Folder_path and you’re sorted.

Sorry my fault, of course my variable Folder_path is your the_path

the line:


   tell application "Finder" to tell (get items of Folder_path) to set {range, first_file_path, first_file_name, extension} to {count it, item 1, name of item 1, name extension of item 1}
.

is the same as


tell application "Finder"
	set a to items of Folder_path
	set range to count a
	set first_file_path to item 1 of a
	set first_file_name to name of item 1 of a
	set extension to name extension of item 1 of a
end tell
.