Can a AppleScripts call a Unix software ?

chevy

Marvelous Da Vinci
Staff member
Mod
I would like to create 3 AppleScripts that have the following function when I drop a file on it (and update the extension accordingly):

Script 1) Create a DVI file by calling "tex" when I drop a TeX file on it.
Script 2) Create a PS file by calling "dvips" when I drop a DVI file on it.
Script 3) Create a PDF file by calling "ps2pdf" when I drop a PS file on it.
 
Just Just for completion, you can call almost anything in the subsystem from an Applescript with

Code:
try
    do shell script "insert_code_here"
end try
 
Is the source of /System/Library/Printers/Libraries/./convert available ?
 
chevy said:
Is the source of /System/Library/Printers/Libraries/./convert available ?

What are you trying to accomplish? If you need some PDF conversion, check out Fink or freshmeat.net, there are several readily-available CLI apps that do a lot of stuff that you can invoke from AS. What does that 'convert' do¿? It might be in CUPS...


dani++
 
What do I do wrong ?

AppleScript Code:
Code:
on processFile(thePOSIXFileName)
	try
		set terminalCommand to ""
		set convertCommand to "/sw/bin/dvipdf "
		set newFileName to thePOSIXFileName & ".pdf"
		set terminalCommand to convertCommand & "'" & thePOSIXFileName & "'"
		
		do shell script terminalCommand
	end try
end processFile

EventLog
Code:
tell current application
	choose file with prompt "Select a file to convert to PDF:"
		alias "Paul_Dirac:Users:michel:Desktop:TeX help:TeX essais:first_sentence.dvi"
	info for alias "Paul_Dirac:Users:michel:Desktop:TeX help:TeX essais:first_sentence.dvi"
		{name:"first_sentence.dvi", creation date:date "Lundi, 10 mai 2004 21:38:49", modification date:date "Lundi, 10 mai 2004 21:38:49", icon position:{0, 0}, size:256.0, folder:false, alias:false, name extension:"dvi", extension hidden:false, visible:true, package folder:false, displayed name:"first_sentence.dvi", default application:alias "Paul_Dirac:Applications_X11:MacDviX_Folder:MacDviX.app:", kind:"Dvi File", locked:false, busy status:false}
	do shell script "/sw/bin/dvipdf '/Users/michel/Desktop/TeX help/TeX essais/first_sentence.dvi'"
end tell

The following code does the trick successfully in Terminal:
Code:
/sw/bin/dvipdf '/Users/michel/Desktop/TeX help/TeX essais/first_sentence.dvi'
 
Updated code (shorter to write, but not better execution):
Code:
on processFile(thePOSIXFileName)
	try
		set terminalCommand to ""
		set convertCommand to "/sw/bin/dvipdf "
		set terminalCommand to convertCommand & quoted form of thePOSIXFileName
		
		do shell script terminalCommand
	end try
end processFile
 
It might be informative to have the script capture & display "the result" after your 'do shell script'. When I get near my Mac, maybe I can try it too
 
I placed flags to follow the software:
Code:
on processFile(thePOSIXFileName, newFileName)
	try
		set convertCommand to "/sw/bin/dvipdf "
		say "3"
		set terminalCommand to convertCommand & quoted form of thePOSIXFileName & " " & quoted form of newFileName
		say "8"
		
		do shell script terminalCommand
		say "9"
	end try
end processFile

EventLog:
Code:
tell application "Finder"
	get selection
		{}
end tell
tell current application
	choose file with prompt "Select a file to convert to PDF:"
		alias "Paul_Dirac:Users:michel:Desktop:TeX help:TeX essais:first_sentence.dvi"
	info for alias "Paul_Dirac:Users:michel:Desktop:TeX help:TeX essais:first_sentence.dvi"
		{name:"first_sentence.dvi", creation date:date "Lundi, 10 mai 2004 21:38:49", modification date:date "Lundi, 10 mai 2004 21:38:49", icon position:{0, 0}, size:256.0, folder:false, alias:false, name extension:"dvi", extension hidden:false, visible:true, package folder:false, file type:"
 
I placed flags to follow the software:
Code:
on processFile(thePOSIXFileName, newFileName)
	try
		set convertCommand to "/sw/bin/dvipdf "
		say "3"
		set terminalCommand to convertCommand & quoted form of thePOSIXFileName & " " & quoted form of newFileName
		say "8"
		
		do shell script terminalCommand
		say "9"
	end try
end processFile

EventLog:
Code:
tell application "Finder"
	get selection
		{}
end tell
tell current application
	choose file with prompt "Select a file to convert to PDF:"
		alias "Paul_Dirac:Users:michel:Desktop:TeX help:TeX essais:first_sentence.dvi"
	info for alias "Paul_Dirac:Users:michel:Desktop:TeX help:TeX essais:first_sentence.dvi"
		{name:"first_sentence.dvi", ...}
	say "3"
	say "8"
	do shell script "/sw/bin/dvipdf '/Users/michel/Desktop/TeX help/TeX essais/first_sentence.dvi' '/Users/michel/Desktop/TeX help/TeX essais/first_sentence.pdf'"
end tell

It doesn't reach flag "9" !
 
Try adding an error handler to your script, and see if it displays an error message. Like so:

Code:
on processFile(thePOSIXFileName, newFileName)
	try
		set convertCommand to "/sw/bin/dvipdf "
		say "3"
		set terminalCommand to convertCommand & quoted form of thePOSIXFileName & " " & quoted form of newFileName
		say "8"
		
		do shell script terminalCommand
		say "9"
		
	on error error_message number errNum
		beep
		display dialog "Error " & errNum & ":" & error_message ¬
			buttons {"OK"} default button 1
		
	end try
	
end processFile
 
Thanks for your help, here is it my solution.

Code:
on processFile(thePOSIXFileName, newFileName)
	try
		set convertCommand to "dvipdf "
		set terminalCommand to convertCommand & quoted form of thePOSIXFileName & " " & quoted form of newFileName
		
		do shell script ("PATH=/sw/bin:$PATH;  " & terminalCommand)
	on error erreur
		display dialog erreur
		
	end try
end processFile
 
For those who are interested here are my two little AppleScripts.
Convert TeXtoDVI compiles a TeX file to a DVI file calling /sw/bin/tex
Convert DVItoPDF converts the TeX generated DVI to PDF calling /sw/bin dvipdf
It expects you have loaded the TeTeX application using Fink.
You can drag and drop you files or complete folders on the AppleScripts to do the conversions.

As far as I know, TeX requires a file name without spaces or other special characters. Spaces in the file path are ok as I implemented a "cd" before calling TeX.

Comments welcome.
 

Attachments

  • TeX convert.zip
    58.1 KB · Views: 0
Back
Top