Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Combining the hints, and making it more secure' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Combining the hints, and making it more secure
Authored by: klktrk on Oct 02, '07 12:42:08AM

Taking the hints above into account, I made an Automator application (or workflow), like so. My goal here was to build a more secure version of the hints above. It's not a good idea to leave pdfs lying around in /private/tmp. So I both made sure to store working files a little more securely, and to clean them up afterwards.

Also, I overrode the built in PDF combine pdfs action, since it outputs its result to the /private/tmp folder as well. I made it output its pdf to the Desktop instead:

  1. drop in Get selected Finder items action
  2. drop in the Finder sort items action
  3. drop in the Automator: run applescript item
  4. drop the code below into the applescript item
  5. drop in the Finder open items in Finder action

Here's the AppleScript, based on the great script developed by Rhet Turnbull in his Convert Images to Combined PDF workflow, upon which this comment is based.


on run {input, parameters}
	set selected_pdfs to {}
	set pic_names to ""
	--make unique md5 hash out of combined string of input files
	tell application "Finder" to set the selected_pic_names to input
	repeat with i from 1 to the count of selected_pic_names
		set pic_name to (item i of selected_pic_names)
		set pic_names to pic_names & pic_name
	end repeat
	set hash to (do shell script "/sbin/md5 -q -s " & quoted form of pic_names)
	set uid to (do shell script "/usr/bin/id -u")
	set tmp_path to "/private/tmp/" & uid & "/" & hash & "/"
	set out_path to POSIX path of (path to desktop folder)
	set out_pdf to out_path & hash & ".pdf"
	set pdfs to ""
	set mkdir to "/bin/mkdir -p -m 755 " & tmp_path & " ; /bin/chmod 700 " & tmp_path & " ; /usr/sbin/chown " & uid & ":" & uid & " " & tmp_path
	set rmdir to "/usr/bin/cd " & tmp_path & " ; /bin/rm -Rf " & tmp_path --need to cd here for safety's sake. don't want to wipe drive if odd bug should occur
	try
		do shell script mkdir
	on error
		display dialog "There was an error running this command: " & mkdir with icon caution
		quit
	end try
	
	tell application "Finder" to set the selected_pics to input
	repeat with i from 1 to the count of selected_pics
		set picitem to (item i of selected_pics) as alias
		set in_pic to POSIX path of picitem
		set outpic to POSIX file in_pic
		try
			set text item delimiters to ":"
			set file_name to last text item of (alias outpic as text)
			set text item delimiters to ""
		on error
			set text item delimiters to ""
		end try
		set outpic to tmp_path & file_name
		set outpic to (removeExtension(outpic)) & ".pdf"
		do shell script "/System/Library/Printers/Libraries/./convert" & " -f " & quoted form of in_pic & " -o " & quoted form of outpic
		
		set outpic_name to alias (POSIX file outpic)
		set selected_pdfs to selected_pdfs & outpic_name
		set pdfs to pdfs & quoted form of outpic & " " -- construct files to pass to python script
	end repeat
	set combine_script to "/usr/bin/python '/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py' -o  " & out_pdf & " " & pdfs
	try
		do shell script combine_script
	on error
		display dialog "Could not combine the pdf files. Python script failed." with icon caution
	end try
	--clean up
	try
		do shell script rmdir
	on error
		display dialog "There was an error while trying to remove temporary files with this command: " & rmdir with icon caution
	end try
	return alias (POSIX file out_pdf) -- send combined pdf to next action
	--	return selected_pdfs
end run

-- remove extension
on removeExtension(in_pic)
	set outpic to {}
	set AppleScript's text item delimiters to "."
	return first text item of in_pic
end removeExtension

---
The Apotek
http://theapotek.com
The Executioner's Summary:
http://www.last.fm/label/Broken+Hill+Music/playlists/6761?autostart

[ Reply to This | # ]

Combining the hints, and making it more secure
Authored by: Krioni on Jan 19, '11 02:40:54PM
That removeExtension function is potentially a problem. If your file's path has a dot anywhere other than just before the file extension, the result will be very wrong.

As an example, the following:

removeExtension( "/Users/someguy/Desktop/file.with.dots.tiff" )
would return:
"/Users/someguy/Desktop/file"
Even worse if there is a dot somewhere higher in the path:
removeExtension( "/Users/someguy/Documents/folder.with.dots/somefile.tiff" )
would return:
"/Users/someguy/Documents/folder"
Here's a replacement:
-- remove extension
on removeExtension(in_pic)
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
	set out_pic to (text items 1 thru -2 of in_pic) as string
	set AppleScript's text item delimiters to oldDelims
	return out_pic
end removeExtension
---
http://www.danshockley.com


[ Reply to This | # ]