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

Click here to return to the 'Ability to choose output file name and more' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Ability to choose output file name and more
Authored by: Krioni on Jan 17, '10 10:12:55PM
OK. So, Sveto pointed out that it would be nice to be able to choose an output folder. So, I rewrote the code for this hint using AppleScript. When creating your workflow, instead of using the "Run Shell Script" command, use the "Run AppleScript" command instead. In the box for the AppleScript code, paste the following:

on run {input, parameters}
	set homePosix to POSIX path of (path to home folder) as string
	set desktopPath to (path to desktop) as string
	set multivalentPosix to homePosix & "bin/pdftools/Multivalent.jar"
	set useOutputFolder to true -- use the named folder as the default save location
	set outputFolderPath to desktopPath & "PDF" & ":" -- a Mac path, not Posix
	-- can ignore outputFolderPath if you set useOutputFolder to false
	set askFileName to true -- ASK for an output file name (and folder). 
	-- If false, uses the default document name generated by Mac OS X
	-- in the default folder (specified above or Desktop)
	if useOutputFolder then
			alias outputFolderPath
		on error -- does not yet exist, so create folder
			set outputFolderName to getNameFromMacPath(outputFolderPath)
			tell application "Finder" to make new folder at pathAlias 
				with properties {name:outputFolderName}
		end try
	else -- default is just desktop
		set outputFolderPath to desktopPath
	end if
	repeat with oneItem in input
		set itemPath to (oneItem as string)
		set itemName to getNameFromMacPath(itemPath)
		set outputPath to outputFolderPath & itemName
		if askFileName then
			tell me to activate
			set outputPath to (choose file name with prompt 
				"Choose a place to save your compressed PDF:" default name itemName 
				default location (alias outputFolderPath)) as string
		end if
		set outPosix to POSIX path of outputPath
		-- tool.pdf.Compress writes the file to the same path as the input file, but with "-o.pdf":
		set compressedPath to text 1 thru -5 of itemPath & "-o.pdf"
		-- get all but ".pdf" to find temp output file name/path
		set compressCommand to "java -classpath " & quoted form of multivalentPosix & 
			" tool.pdf.Compress -compatible -noembed -noalt -jpeg " & 
			quoted form of POSIX path of itemPath
		-- compress the PDF created by Mac OS X:	
		do shell script compressCommand
		-- now, move the compressed PDF to the desired location:
		do shell script "mv " & quoted form of POSIX path of compressedPath 
			& " " & quoted form of outPosix
	end repeat
	return input
end run

on getNameFromMacPath(macPath)
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set nameFromPath to last text item of macPath
	set AppleScript's text item delimiters to oldDelims
	return nameFromPath
end getNameFromMacPath
NOTE: There are PREFERENCES included in the code above. They allow you to specify the location of your Multivalent JAR file, assuming you want it in a sub-folder of your user folder. If that is wrong, you can remove the 'homePosix &' part and just specify the whole Posix path to the JAR file. You can also choose a default outputFolderPath for the compressed PDF - if you don't it defaults to using the Desktop. Also, the code above will ask you for a file name, you can set askFileName to false to just use the name Mac OS X comes up with. I hope this is useful. I look forward to using it, since it seems to save quite a bit of space in PDFs. Thank you to Sveto for getting us the info about Multivalent!

[ Reply to This | # ]
Ability to choose output file name and more
Authored by: Sveto on Jan 19, '10 08:17:45AM

Thank you very much Krioni. That is exactly what I was requesting. I have only 1 question. I was unable to understand where the temporary uncompressed file was located and if it was deleted after the compression. Did I miss something?

[ Reply to This | # ]
Ability to choose output file name and more
Authored by: Krioni on Jan 19, '10 10:26:39AM

Mac OS X creates the temp file itself in a temp directory that gets cleaned out automatically after a while. When you choose "Preview" from the print dialog, it's the temp file that is opened by Interestingly enough, Apple made the usual command-click-on-window-title-to-see-file-path trick NOT work when you do this in However, if you toss an alias to Skim (or some other PDF reader) in your PDF Services folder, and send the PDF there instead of, you'll be able to command-click the window titlebar to see where it creates the temp file.

That meant I didn't have to clean up the temp file, since it is handled in the Mac OS X approved manner. In your original hint, you may have been able to do the same thing. I think $1 is the path to the temp file, so perhaps you could have just operated on it instead of using cat and a redirect to send it to your temp location. Of course, you'd then need to "mv" the output file (and perhaps remove the "-o" that gets tacked into the file name) to your desired destination.


[ Reply to This | # ]