Shell commands

Hello,

In fact, there is a folder on my desktop with the name “Pdfs”, and i put regularily folders with images files inside this main folder. (these subfolders can be 5 or 20 folders, everyone with 10 or 30 images)
So, i’ve tried to write a applescript to clean up with all this files, but for now, not even the simple conversion of one picture file gives results.
I cant realize how to:

1) convert a single picture file
2) merge image files (30 or 100) of a given folder into one unified PDF file.
…via shell script. Here is my first code:

tell application "Finder"
	set results_folder to ((path to desktop) & "Pdfs" as text)
	repeat with i from 1 to count items in front window
		set this_item to item i of front window
		if class of this_item is folder then
			set new_name to name of this_item
			set source_file to (this_item as alias)
			--display dialog (source_file & return & return & new_name & return & return & (folder of front window as alias) as text)
			my process_item(source_file, new_name, results_folder)
		end if
	end repeat
end tell

on process_item(source_file, new_name, results_folder)
	-- NOTE that the variable this_item is a file reference in alias format 
	try
		set the source_item to the quoted form of the POSIX path of the source_file
		set the target_path to the quoted form of the POSIX path of (((results_folder as string) & new_name) as string)
		with timeout of 900 seconds
			do shell script ("pstopdf " & source_item & " -o " & target_path)
		end timeout
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_item

Suggestions, tutorials and so forth are welcome. Thanks.

Hi,

your script doesn’t work, because you’re passing only folders
and pstopdf expects files.
This is a simpler version of the script. The generated files are saved in the same folder as the files come from.
To retrieve the files from a window is not very reliable. It’s better to take them from a folder.
The timeout block is ineffective, it doesn’t affect shell scripts.


set results_folder to ((path to desktop as text) & "Pdfs")
tell application "Finder" to set sourceFiles to files of folder results_folder
repeat with oneFile in sourceFiles
	process_item(oneFile as alias)
end repeat

on process_item(source_file)
	-- NOTE that the variable this_item is a file reference in alias format 
	try
		do shell script ("pstopdf " & quoted form of POSIX path of source_file)
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_item

FWIW, this was my first pass (without any testing):

set results_folder to POSIX path of (path to desktop as Unicode text) & "Pdfs:"

-- `Finder window` is more specific; `window` could include the preferences window, etc.
tell application "Finder" to (folders of front Finder window) as alias list

repeat with this_item in result
	try
		tell application "Finder" to set new_name to name of this_item
		
		do shell script "/usr/bin/pstopdf " & ¬
			quoted form of POSIX path of this_item & ¬
			" -o " & quoted form of (results_folder & new_name)	
	on error errMsg number errNum
		tell application "Finder"
			activate
			display alert "Error " & errNum message errMsg buttons {"Cancel Script", "Skip Item"} cancel button 1 default button 2 giving up after 120
		end tell
	end try
end repeat

Thanks for your quick answer, StefanK- (great your “lightweight” solution)
After re-reading the shell code, i realized that ‘pstopdf’ must stay by postscript to pdf conversion. (Please… my knowledge in shell scripts is not so big.)
But i wand not this kind of conversion. Grr…!
I’ve overlooked that. How already written its my target to compress and to merge images (JPG) into one PDF -file. But for now, its a rebus for me, from where i can start from… 8P

Thanks to you too, Bruce. But i overlooked that the shell-command is not the right one.
Somebody will continue to answer to my real question? Help!

The proper command is /System/Library/Printers/Libraries/convert, which is actually /usr/sbin/cupsfilter.
I wrote (or probably “borrowed”) once an Automator action to convert various data types to PDF using this executable
Hope it helps

property type_list : {"JPEG", "GIFf", "PICT", "TIFF", "PDF", "TEXT"}
property extension_list : {"jpg", "gif", "pct", "tif", "pdf", "rtf"}

on run {input, parameters}
	try
		repeat with i in input
			if name extension of (info for i) is in extension_list then
				set orgFileName to POSIX path of i
				set convertCommand to "/System/Library/Printers/Libraries/convert"
				set newFileName to orgFileName & ".pdf"
				set terminalCommand to convertCommand & " -f " & quoted form of orgFileName & " -o " & quoted form of newFileName & " -j \"application/pdf\""
				do shell script terminalCommand
			end if
		end repeat
		return input
	end try
end run

Great! thats a good catch! :smiley:
i’ll try to make a applescript from this code, because the automator is too clumsy for me.
…I remember the first time (on panther) it has seemed to another good applescript -resource, but now, i recognized that the Automator is much more disappointingly. Sob.

i’ve another issue to discuss:
how can i do, to read a simple txt- document in the home path, without using the “Text editor”?

It comes more complex: Toast Titanium or a unix-command has to read every paragraph of this txt-document,
[adding these paragraphs (paths of files which should be burned) to the main window of the Burn Data Disc-window (of Toast Titanium)]<- this is not a problem

I seach solutions for more independence in my scripts. Thanks for any suggestion. :cool: