Use Automator to perform actions on odd or even numbered files.

Here is the delima.
I have a pdf that I have to turn into jpgs for a flash slide show.
The odd pages in the pdf have different margins than the even pages and I want to crop the margins off.
I’ve set up 3 automator actions. The first one renders each pdf page to a .jpg image and renames it sequentially like this; pg01.jpg, pg02.jpg, pg03.jpg, pg04.jpg etc. That works great.
The problem starts at the next step. I’ve set up another automator droplet to perform some actions on the odd numbered files using the excellent photoshop automator actions from completedigitalphotography.com. I’ve also set up another automator droplet to perform very simmilar actions on the even numbered pages. Currently I have to open the folder containing the .jpgs, command click to select all of the odd numbered files, drag them onto the “odd” automator droplet, then go back to the folder command click to select all of the event numbered files, drag them onto the “even” automator droplet.

How can I build this filtering of the file names it one automator action?

  1. Render the pages as jpgs.
  2. perform actions on the odd numbered pages.
  3. perform actions on the even numbered pages.

I’ve tried to find a way to use an applescript to tell the finder to only select the odd numbered files, but if I can do that, how do I pass this to automator to perform actions on only those files.
Then it would be;

  1. Render the pages as jpgs.
    A. Run applescript to select only odd numbered pages. Pass those to next step.
  2. perform actions on the odd numbered pages.
    B. Run applescript to select only even numbered pages. Pass those to next step.
  3. perform actions on the even numbered pages.
    Thanks for any help you can give…
    Jim

Model: MBP and PMG4 MDD
Browser: Firefox 1.5
Operating System: Mac OS X (10.4)

Jim:

This should get you started. The script analyzes the filenames, and creates two lists, one for even numbered filenames and one for odd. As long as you preserve the container for each filename (which this script does), each list becomes a list of paths to all the even or odd numbered files from the chosen folder:

set disp_pict to ((choose folder) as text)
tell application "Finder" to set all_File_names to the name of every file in folder disp_pict
set {even_List, odd_List} to {{}, {}}
repeat with aa in all_File_names
	set final_dig to character ((offset of "." in aa) - 1) of aa
	if (final_dig mod 2) = 0 then
		set end of even_List to (disp_pict & (aa as text))
	else
		set end of odd_List to (disp_pict & (aa as text))
	end if
end repeat

Good luck, I hope this helps,

Thanks Craig,
I’ll give that a try.

I know I’ve seen in the forums how to pass variables to automator…
let the search begin…

-Jim

Jim:

It appears that you may be stuck with continuing with two Actions; I cannot get the Get Value of Variable to work correctly for this workflow. Here is what I tried:

-Ask for Finder Items (I then chose a folder of images with names like this: DispImag002.jpg)
-Set Value of Variable (I called the variable ChosenFolder)
-Get Folder Contents
-Run AppleScript

Here is the AS code for that action:

on run {input, parameters}
	set {odd_List, even_List} to {{}, {}}
	repeat with each_1 in input
		set final_dig to character ((offset of "." in (each_1 as text)) - 1) of (each_1 as text)
		if (final_dig mod 2) = 0 then
			set end of even_List to (each_1 as text)
		else
			set end of odd_List to (each_1 as text)
		end if
	end repeat
	return odd_List
end run

-Set Contents of TextEdit Document (Just to be sure the file paths were correct)
-Get Value of Variable (Again, the ChosenFolder variable)
-Get Folder Contents
-Run AppleScript

I then tried using the same AS code, asking for the even_List returned. What is infuriating is that the Get Value of Variable indeed returns the correct folder, originally chosen above, but the Get Folder Contents cannot seem to read the folder at all. Instead, it returns this error:

The Action “Get Folder Contents” was not supplied with the required data.

I have no clue why it happens.

Craig,
This does work if I drag the files to the automator application. It performs the actions on the odd files.
Here is what I have set up. (I haven’t had the time to do more than copy and paste your script and try it)

In automator
1 Get Specified Finder Items
2 Run Applescript (your script copied and pasted in)
3 Run the Photoshop actions.

It runs the actions on the odd numbered files.

This is a great start. Hopefully I’ll have a little more time to play with it.

Thanks,

-Jim

Oh, BTW If I try to drop a folder of images on the automator app, it just crashes while trying to run the applescript.

Craig,
I don’t have the
“Set Value of Variable”
option. Maybe that’s a Leopard thing? I’m using Tiger.

I have most of it working by running the script you provided.

-Ask for Finder Items
-Get Folder Contents
-Run Applescript (Gets the even numbered files)
-Do the photoshop stuff
-Run Workflow (opens another workflow which is the exact same except it runs the odd page photoshop stuff on the odd numbered files.)

I would love to be able to just drop a folder full of images on this app and have it output the files to another folder with out any other user action. I think that we are limited by what automator can’t do in this case.

Thanks again for you help.
-Jim

Another thought.
It’s possible to set the contents of a text file to the results of the applescript you wrote. I can even do that with automator.

Is it possible to set the contents of a new text file “even.txt” to the contents of the variable even_List and “odd.txt” to the contents of the variable odd_List?

Then use another applescript to get the file list from even.txt and pass that list on to automator. Then use the applescript to get the contents of odd.txt and pass that list to automator.

-jim

Jim:

Right, Automator for Tiger has fewer options for variables, if I recall correctly. I have another idea, however, but have only tested it minimally. Could you please check your version of Automator to see if you can Save As Plug-In… and can also choose Plug-in For Folder Actions?

If so, here is what I propose. Create two folders, one for odds and one for evens. Attach the Automator workflows for the Photoshop stuff to the appropriate folders (odd or even) as Folder Actions. Then, we generate a droplet using AS to either move or copy the JPG files to the correct folders, and although it may be a bit sluggish (and loud), it should all work nicely.

Let me know if this is a workable alternative for you.

The version of Automator that I have is 1.0.5. Yes, I can save as a plug in, but I need this work flow to be something that I can give to a user and they can either drag and drop a folder on or just double click to run it. I believe your suggestion would work, but I would have to rely on the user not changing the folders etc.
Ideally I would like a solution that can create / delete any folders / file necessary so the user just starts with a folder full of image files and ends up with another folder full of the processed files.
See the next post.

Here is what I’m working on now…
-Ask for finder items
-Get folder contents
-run this applescript to get a list of files in the folder to a variable

on run {input, parameters}
	set master_List to {}
	repeat with each_1 in input
		set end of master_List to (each_1 as text)
	end repeat
	return master_List
	return input
end run

-New Text File (TextEdit action to create a file called temp.txt) If I have this list in a text file, I can access it again from automator.
I now have a text file with this as the contents

-Run this applescript to get the list back into a variable so that I can get just the even numbered files.

on run {input, parameters}
	set the_List to {}
	set theFile to alias "path:to:user:Desktop:temp.txt"
	set input to every paragraph of (read theFile as string)
	return input
end run

This returns a list with some extra characters in it. I can’t seem to post it in here so I’ll type out the character names…
(left curly bracket)(quotation mark)(upside down comma)(carrot)
path(colon)to(colon)file01.jpg(quotation mark)(comma)(quotation mark)path(colon)to(colon)file02.jpg(quotation mark)(comma)(quotation mark)path(colon)to(colon)file03.jpg(quotation mark)(comma)(quotation mark)etc until we get to the end of the list and then (space)(quotation mark)(quotation mark)(right curly bracket)
There are extra characters in this list now because of getting it from the text file.
How can I get the list from the text file without the extra stuff?

-Jim

Jim:

I have had problems with that method as well. If your version of Automator as a Do Shell Script action, use that right after the Ask For Finder Items and set the Pass Input: to as arguments. Then, put this into the space:

find “$@” -name “*.jpg”

That will provide a clean set of file paths to the TextEdit document.

Ok, I’ve decided it might be best to use the clipboard to hold the master list.
Here’s what I have so far.
-Ask for Finder Items
-Get Folder Contents
produces this in the “View Results” action.

-Copy to Clipboard
produces this in the “View Results” action.

-Get Clipboard Contents (plugin from http://www.automatedworkflows.com)
produces this in the “View Results” action.

How do I get the “Macintosh_HD…” to be alias “Macintosh_HD…”?
I need to change the text paragraphs to a list of file paths.
Any Ideas?

Answered my own question…
Getting very close now.
-Run Applescript (initialize the clipboard) don’t really like doing this just incase there is something there, but I had a couple of issues if I didn’t do this first.

on run {input, parameters}
	tell application "Finder"
		set clipboard to ""
	end tell
	return input
end run

-Ask For Finder Items
-Get Folder Contents
-Copy to Clipboard
-Run Applescript

on run {input, parameters}
	set {odd_List, even_List} to {{}, {}}
	repeat with each_1 in input
		set final_dig to character ((offset of "." in (each_1 as text)) - 1) of (each_1 as text)
		if (final_dig mod 2) = 0 then
			set end of even_List to (each_1 as text)
		else
			set end of odd_List to (each_1 as text)
		end if
	end repeat
	return odd_List
end run

-do photoshop stuff on odd numbered files
-Get Clipboard Contents (Automator plugin from http://www.automatedworkflows.com)
-Run Applescript

on run {input, parameters}
	set master_List to {}
	set i to ""
	repeat with i from 1 to (count of paragraphs in input)
		set end of master_List to (paragraph i of input as alias)
	end repeat
	return master_List
end run

-Run Applescript

on run {input, parameters}
	set {odd_List, even_List} to {{}, {}}
	repeat with each_1 in input
		set final_dig to character ((offset of "." in (each_1 as text)) - 1) of (each_1 as text)
		if (final_dig mod 2) = 0 then
			set end of even_List to (each_1 as text)
		else
			set end of odd_List to (each_1 as text)
		end if
	end repeat
	return even_List
end run

-do photoshop stuff on even numbered files
I’ll refine this a little more so that I can drag a folder full of sequentially numbered files on to it, but I think we’re close.

Do you see anything that I might do to improve / streamline it?

-Jim

Jim:

I do not understand the need for the Run AppleScript action that you have after the second clipboard reading. (The one with the master_List variable.)

I do not know if this will speed it up, but change the first Run AppleScript after the first clipboard read to this:

on run {input, parameters}
	set odd_List to {}
	repeat with each_1 in input
		set final_dig to character ((offset of "." in (each_1 as text)) - 1) of (each_1 as text)
		if (final_dig mod 2) ≠ 0 then set end of odd_List to (each_1 as text)
	end repeat
	return odd_List
end run

It now only creates the odd_List, and only saves those, instead of generating both lists. Here is the same modification for the last Run AppleScript to generate only the even_List:

on run {input, parameters}
	set even_List to {}
	repeat with each_1 in input
		set final_dig to character ((offset of "." in (each_1 as text)) - 1) of (each_1 as text)
		if (final_dig mod 2) = 0 then set end of even_List to (each_1 as text)
	end repeat
	return even_List
end run

I would like to know if those actually make it go any faster, but I am doubtful that they will.

Craig. When I get the contents of the clipboard it returns as text and I need it to be a list of file paths. That applescript turns the text of

Into a list of paths (alias’s) like this.

That I can pass to your applescript.
I suppose I could combine those applescripts into one to streamline it some.
I’ll try your refined scripts and see what happens.
Thanks again
-Jim

Ok, I think I have the final working workflow.
I am using 2 workflows. The first one takes a multipage pdf document and extracts each page to a .jpg image. Both of these automator actions are saved as applications. The first action needs to have the pdf file drag and dropped onto it.
-Ask for Confirmation
“Drag a pdf file onto this Icon to save each page as an image.”
“You will have to choose a size and then you will be prompted for file name and location options.”
-Get Specified Finder Items
-Run Applescript - This is an error control.

	tell application "Finder"
		if input = {} then
			display dialog "This only works if you drag a pdf onto it." with icon stop ¬
				buttons "cancel"
		else
			return input
		end if
		
	end tell

-Render PDF Pages as Images
-Rename Finder Items (Make Finder Item Names Sequential) - renames files starting at “pg01.jpeg”
-Rename Finder Items (Replace Text in Finder Item Names) - replaces “.jpeg” with “.jpg” Finder automatically names the files with the extension “.jpeg”, but I need it to be “.jpg”.
-New Folder - Asks user to create a new folder where the images are placed.

The next workflow application works when the user drag and drops this folder full of images onto it.
-Run Applescript - Resets the clipboard -(erases the clipboard - Be careful)

tell application "Finder"
		set clipboard to ""
	end tell
	return input

-Get Specified Finder Items
-Get Folder contents
-Run AppleScript - error control

tell application "Finder"
		if input = {} then
			display dialog "This only works if you drag and drop a folder on to it with sequentially named image files. For example img01.jpg img02.jpg etc." with icon stop ¬
				buttons "Cancel"
		end if
	end tell
	return input

-Copy to Clipboard
-Run AppleScript

	set odd_List to {}
	repeat with each_1 in input
		set final_dig to character ((offset of "." in (each_1 as text)) - 1) of (each_1 as text)
		if (final_dig mod 2) ≠ 0 then set end of odd_List to (each_1 as text)
	end repeat
	return odd_List

-At this point I perform actions on the odd numbered files using the photoshop actions from www.completedigitalphotography.com
-Get Clipboard Contents - This plugin comes from http://www.automatedworkflows.com
-Run AppleScript - This applescript takes the contents of the clipboard and turns it into a list of file paths for the next applescript.

	set master_List to {}
	--set textFile to input as string
	set i to ""
	repeat with i from 1 to (count of paragraphs in input)
		set end of master_List to (paragraph i of input as alias)
	end repeat
	return master_List

-Run AppleScript

	set even_List to {}
	repeat with each_1 in input
		set final_dig to character ((offset of "." in (each_1 as text)) - 1) of (each_1 as text)
		if (final_dig mod 2) = 0 then set end of even_List to (each_1 as text)
	end repeat
	return even_List

-At this point I perform actions on the even numbered files using the photoshop actions from www.completedigitalphotography.com
-Get Clipboard Contents - This plugin comes from http://www.automatedworkflows.com
-Run AppleScript - This applescript takes the contents of the clipboard and turns it into a list of file paths for the next Photoshop actions. This list is once again both even and odd files.
-At this point photoshop actions are performed to create thumbnail images.

One problem with the first action is that If you run the application again to create another folder full of images from a pdf, it names the files starting with the last number from the last time you ran the action. In other words, if you ran the application on a 24 page pdf you would get a folder full of .jpg named “pg01.jpg” to “pg24.jpg” If you then run it on another 10 page pdf you will get .jpgs named “pg26.jpg” to “pg35.jpg”. Not “pg01.jpg” to “pg10.jpg”. The only way I’ve found around this is to sequentially name the files differently such as “page01.jpg”.

I’d like to thank Craig for his excellent applescript. I’ve tried getting this working for quite a while and couldn’t have made it work without his help.

-Jim

Model: MBP and PMG4 MDD
Browser: Camino 1.6.4
Operating System: Mac OS X (10.4)