# Can a AppleScripts call a Unix software ?



## chevy (May 10, 2004)

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.


----------



## brianleahy (May 10, 2004)

Definitely doable.  Check it out:   

http://developer.apple.com/technotes/tn2002/tn2065.html


----------



## chevy (May 10, 2004)

I see that I can edit the "Convert To PDF" script !!!


----------



## michaelsanford (May 10, 2004)

Just Just for completion, you can call almost anything in the subsystem from an Applescript with


```
try
    do shell script "insert_code_here"
end try
```


----------



## chevy (May 11, 2004)

Is the source of /System/Library/Printers/Libraries/./convert available ?


----------



## dani++ (May 12, 2004)

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++


----------



## chevy (May 14, 2004)

What do I do wrong ?

AppleScript 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

```
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:

```
/sw/bin/dvipdf '/Users/michel/Desktop/TeX help/TeX essais/first_sentence.dvi'
```


----------



## chevy (May 14, 2004)

Updated code (shorter to write, but not better execution):

```
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
```


----------



## brianleahy (May 14, 2004)

What's your result?  Nothing?  Error code?


----------



## chevy (May 14, 2004)

nothing, not even an error code


----------



## brianleahy (May 14, 2004)

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


----------



## chevy (May 14, 2004)

TheResult is empty.


----------



## chevy (May 14, 2004)

I placed flags to follow the software:

```
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:

```
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:"
```


----------



## chevy (May 14, 2004)

I placed flags to follow the software:

```
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:

```
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" !


----------



## brianleahy (May 14, 2004)

Try adding an error handler to your script, and see if it displays an error message.    Like so:


```
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
```


----------



## chevy (May 15, 2004)

Thanks for your help, here is it my solution.


```
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
```


----------



## chevy (May 15, 2004)

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.


----------

