Need to convert .TGA files to .JPG - Wierd hole in Automator.

I need to use Automator convert files from .TGA to .JPG.
Automator does not support .TGA for some reason, yet:
I can view .TGA in Preview
I can use Automator to open a .TGA in Preview
Preview has a scriptable action to save as another type
Automator indicates the script worked with no errors, yet produces no output
I can save a .TGA in Preview to .JPG with no problem.


This sure looks like a bug in Automator. Any ideas on how to get around this? I have a bunch of files I need to regularly change from .TGA to .JPG, and I'm so close!

iMac duo 20"/2.0GHZ/1x1GB/256M video/250GB HD, Mac OS X (10.4.5), History: 8500 PM, Newt 100, 130, 2000, 2100, G3 imac, 17" G5 imac, 20" G5 iMac

Posted on Mar 4, 2006 11:44 AM

Reply
10 replies

Mar 4, 2006 1:22 PM in response to Spinnetti

You may be surprised to learn that Preview's Automator actions actually have little if anything to do with Preview. Most of the actions are written using the obscure headless app "Image Events" as a back end. The average user has never heard of this application, so Apple just slapped a Preview icon on the actions so people would at least have a clue about what they're for.

This decision creates a bit of disconnect for power users such as yourself. The description of the action is, unfortunately, accurate:
Change Type of Images
This action converts images to the selected image format.
Requires: Image files of type BMP (.bmp), GIF (.gif), JPEG (.jpg or .jpeg), JPEG 2000 (.jp2), PDF (single page .pdf), PICT (.pct or .pict), PNG (.png), or TIFF (.tif or .tiff).

So, sorry, no *.TGA. There may be other scriptable apps or Automator actions, however, that can do what you want. I'll post back if I find something.

Mar 4, 2006 2:35 PM in response to Michael Henley

Thanks for the info. I would have never guessed.
I've been trying all kinds of crazy stuff to get this to work.
So far, I've been converting on my PC and copying over which is a crummy way to do it.

Is there any way to script the Preview functions in applescript for instance?
Even a sort of macro recorder that would open a selection and then do a save as would work. I tried applescript, but the script wouldn't capture any actions inside of the Preview app (another wierdness)..

Thanks,

Mar 4, 2006 3:01 PM in response to Spinnetti

Preview, unfortunately, has zero AppleScript support. Apple justifies this by offering the separate Image Events as a direct interface to the SIPS (Scriptable Image Processing Server) interface that both applications use. Neither application is recordable, so the record button will be of little use here.

I looked in the Image Events dictionary and saw TGA as a format. It would be odd for Apple to leave out functionality in Automator that was available all along in Image Events, but I wouldn't put it past them. Let me mess around with this and see if I can get something to work for you.

Mar 4, 2006 5:39 PM in response to Spinnetti

Good news! Image Events can convert TGA to JPG.

So I wrote the following script with Script Editor, but since this is an Automator forum I wrapped up the code for use with Automator's Run AppleScript action. This provides more flexibility to integrate the script in a larger workflow, and has more saving options (such as Finder plug-in).

The script accepts the results of any actions that return a list of file references and/or aliases. For example, Finder's Get Selected Finder Items or Get Folder Contents. (This step may be omitted if the workflow is saved as a Finder plug-in. In this case Finder will automatically send the current selection to the first action of the workflow.)

The script then creates a new dated folder inside the folder that contains your source images. The source images are copied to the new folder and renamed. Finally Image Events opens each copied file and converts it to JPEG format.

To use the script, drag Automator's Run AppleScript action into your workflow and replace the default code from the body of the action with the following:

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">on run {input, parameters}
set {output, folder_exists} to {{}, false}

tell application "Finder" to repeat with a_file in input
-- 1. Get the original file location and name.
set {source_folder, source_name} to {container, name} of a_file

-- 2. Replace .TGA with .JPG in the file name variable.
tell (a reference to AppleScript's text item delimiters)
set {tid, contents} to {contents, ".tga"}
set {final_name, contents} to {source_name's text items, ".jpg"}
set {final_name, contents} to {final_name as text, tid}
end tell

-- 3. Create a new folder inside the original folder.
if not folder_exists then
tell (current date) to set time_stamp to "TGA->JPG " & short date string & space & it's hours & "." & it's minutes & "." & it's seconds
set final_folder to make new folder at source_folder with properties {name:time_stamp}
set folder_exists to true
end if

-- 4. Copy the original file to the new folder, replacing the original file name with the new file name.
set final_file to duplicate a_file to final_folder
set name of final_file to final_name
set final_file to ((final_folder as alias) as Unicode text) & final_name

-- 5. Open the copied file and convert it to JPG.
tell application "Image Events"
set doc to open file final_file as alias
save doc as JPEG
close doc
end tell

-- 6. Pass the new file locations on to the next action (if any).
set end of output to (final_file as alias)
end repeat

return output
end run</pre>

PowerMac G5 (June 2004) 2x1.8GHz 1.25GB, PowerBook G4 (12-inch DVI) 1x1GHz 768MB Mac OS X (10.4.5)

Mar 4, 2006 6:41 PM in response to Michael Henley

You Rock!

Works as advertised 🙂

Now I'm trying to teach myself how to mod the script a bit.

How I've set it up is to open up my network, reach over to my PC, Move off the TGA files to the Mac, then run your script. What I'd like to do though is to move them over, rename to "PFddmmyy nnn.jpg" (PF + Date + 3 digit sequence number) replacing the originals in the Mac destination folder where I moved them.

You already did the hardest part (I'm trying the rest now) - I really appreciate it!

Mar 4, 2006 6:59 PM in response to Spinnetti

That makes sense. It's easy for me to over-reach when scripting for Automator. In the context of a workflow each action should be focused on one particular task. So assuming you can take care of the other stuff in the in course of your workflow, you can use this stripped down version of the script to re-write and re-name the files in place without making any unnecessary copies:

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">on run {input, parameters}
set output to {}

tell application "Finder" to repeat with a_file in input
-- 1. Get the original file location and name.
set {source_folder, source_name} to {container, name} of a_file

-- 2. Replace .TGA with .JPG in the file name variable.
tell (a reference to AppleScript's text item delimiters)
set {tid, contents} to {contents, ".tga"}
set {final_name, contents} to {source_name's text items, ".jpg"}
set {final_name, contents} to {final_name as text, tid}
end tell

-- 3. Rename the source file.
set name of a_file to final_name
set final_file to ((source_folder as alias) as Unicode text) & final_name

-- 4. Open the copied file and convert it to JPG.
tell application "Image Events"
set doc to open file final_file as alias
save doc as JPEG
close doc
end tell

-- 5. Pass the new file locations on to the next action (if any).
set end of output to (final_file as alias)
end repeat

return output
end run</pre>

PowerMac G5 (June 2004) 2x1.8GHz 1.25GB, PowerBook G4 (12-inch DVI) 1x1GHz 768MB Mac OS X (10.4.5)

Mar 4, 2006 8:39 PM in response to Michael Henley

Well, after about 2 hours of messing around I have it just as I hoped thanks to your help.

I connect to my PC, move off the files, rename them, convert to JPG and stuff them in a docs folder.

One last tiny question - There is an automator action to connect to my PC, but I don't see one to close the connection. I tried the "Eject Disc" action after selecting the server, but no dice.

Thanks!

Mar 4, 2006 9:27 PM in response to Michael Henley

Awesome, I'm glad that worked out so well for you! As
for the last question, assuming your PC is a regular
mounted volume you should be able to set up a Get
Specified Finder Items
action before the
Eject Disk action. I just tested it on my G3
with no problems. I selected the volume name from the
highest level of the Finder.


Duh.. I guess I was trying to make it too complicated.
I'm all done... Thanks again - Apple should hire you!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Need to convert .TGA files to .JPG - Wierd hole in Automator.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.