Opening, Saving, then closing files

I appreciate everyone’s help and time, I’m learning a lot. I hope no one feels like I’m having you guys practically write my script for me, I do spend a lot of time trying to figure this out on my own. I’m just very new at this.

I think I’ve got almost everything working, only a few things left that I can’t figure out.

  1. The list of files I’m selecting; I am unable to figure out how to open just the first one, run the script on the first file, then go back and start over with the second file in the list and so on.
    2)I can’t figure out how to save to the folder I have selected, and how to save by pulling the original file name and adding “1-16”, “2-15”, and so on
    3)I’m sure this is easy, but I must be slow: how do I close the file at the end, leaving the application running?

Thanks!

-- 1)
set chosenfiles to my choosefiles()
if chosenfiles is missing value then
	return
end if
-- 2)
set filepaths to my getfilepaths(chosenfiles)
-- 3)
set outfilenames to my getoutfilenames(chosenfiles)
-- 4)
set OutputFolder to my chooseoutputfolder()
if OutputFolder is missing value then
	return
end if
end run

-- I am asking the user to choose files to process
-- In case the user cancels the dialog, I return «missing value»
on choosefiles()
	try
		set chosenfiles to choose file with prompt "Please choose files to process:" with multiple selections allowed without invisibles
		return chosenfiles
	on error
		return missing value
	end try
end choosefiles

-- I am asking the user to choose an output folder
-- In case the user cancels the dialog, I return «missing value»
on chooseoutputfolder()
	try
		set chosenfolder to choose folder with prompt "Please choose an output folder:" without multiple selections allowed and invisibles
		return chosenfolder
	on error
		return missing value
	end try
end chooseoutputfolder

-- I am turning a list of Finder items (aliases) into a list of file path strings (Mac, not Posix)
on getfilepaths(finderitems)
	set filepaths to {}
	repeat with finderitem in finderitems
		set filepaths to filepaths & (finderitem as Unicode text)
	end repeat
	return filepaths
end getfilepaths

-- I am returning a list of output file names where the extensions are stripped off
on getoutfilenames(finderitems)
	set outfilenames to {}
	repeat with finderitem in finderitems
		set fileinfo to info for finderitem
		set filename to name of fileinfo
		set dotoffset to offset of "." in ((reverse of (characters 1 through -1 of filename)) as Unicode text)
		if dotoffset is greater than 0 then
			set outfilename to ((characters 1 through -(dotoffset + 1) of filename) as Unicode text)
		else
			set outfilename to filename
		end if
		set outfilenames to outfilenames & outfilename
	end repeat
	return outfilenames
end getoutfilenames

tell application "Finder"
	open chosenfiles
	--1 through 16 
	activate application "Adobe Acrobat Professional"
	tell application "System Events"
		tell process "Acrobat"
			click menu item "Batch Processing..." of menu 1 of menu item "Document Processing" of menu 1 of menu bar item "Advanced" of menu bar 1
			select row 14 of outline 1 of scroll area 1 of window "Batch Sequences"
			select static text "Publix 16T Crop 1-16" of row 14 of outline 1 of scroll area 1 of window "Batch Sequences"
			click button "Run Sequence" of window "Batch Sequences"
			click button "OK" of window "Run Sequence Confirmation - Publix 16T Crop 1-16"
			click menu item "Encapsulated PostScript" of menu 1 of menu item "PostScript" of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1
			keystroke (OutputFolder & outfilenames & "1-16")
			click button "Save" of window "Save As"
			delay 5
			click menu item "Revert" of menu 1 of menu bar item "File" of menu bar 1
			delay 2
			click button "Revert" of front window
			click button "Close" of window "Batch Sequences"
		end tell
	end tell
	
	--2 through 15
	tell application "System Events"
		tell process "Acrobat"
			click menu item "Batch Processing..." of menu 1 of menu item "Document Processing" of menu 1 of menu bar item "Advanced" of menu bar 1
			select row 15 of outline 1 of scroll area 1 of window "Batch Sequences"
			select static text "Publix 16T Crop 2-15" of row 15 of outline 1 of scroll area 1 of window "Batch Sequences"
			click button "Run Sequence" of window "Batch Sequences"
			click button "OK" of window "Run Sequence Confirmation - Publix 16T Crop 2-15"
			click menu item "Encapsulated PostScript" of menu 1 of menu item "PostScript" of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1
			keystroke (OutputFolder & outfilenames & "2-15")
			click button "Save" of window "Save As"
			delay 5
			click menu item "Revert" of menu 1 of menu bar item "File" of menu bar 1
			delay 2
			click button "Revert" of front window
			click button "Close" of window "Batch Sequences"
		end tell
		activate application "Finder"
	end tell
end tell

edit: I suppose I should get the POSIX path of the folder I’m choosing as the output, then when Saving As via GUI scripting I could key Shift+Cmd+G and enter the POSIX path along with the stripped file name. What would be the best way to accomplish this? I’m still lost on the others.

Hi drewfus,

Unfortunately I cannot help you with the Acrobat part of your script and I am also not an expert when it comes to GUI scripting. But maybe I can give you some advice about how to iterate over the selected files and how to clean up the script a bit.

Basically if you want to process a bunch of items, then you need to use a «repeat» block:


set compmodels to {"PowerBook", "MacBook", "iBook", "iMac", "MacBook Pro"}

repeat with compmodel in compmodels
	display dialog compmodel
end repeat

Therefor in your case you will need to iterate over the list of «chosenfiles» and process each file using Acrobat. As I don’t have Acrobat installed, the sample script uses Skim, a free PDF viewer. Please note, that I shortened the code for clearness.


on run
	-- 1)
	set chosenfiles to my choosefiles()
	log chosenfiles
	if chosenfiles is missing value then
		return
	end if
	-- 2)
	set filepaths to my getfilepaths(chosenfiles)
	-- 3)
	set outfilenames to my getoutfilenames(chosenfiles)
	-- 4)
	set OutputFolder to my chooseoutputfolder()
	if OutputFolder is missing value then
		return
	end if
	-- IMPORTANT: Here you are processing each file from the list of «chosenfiles» !
	repeat with chosenfile in chosenfiles
		my procfile(chosenfile)
	end repeat
end run

-- I don't have Acrobat installed, so I use Skim instead
-- You can add your Acrobat code here
on procfile(theFile)
	tell application "Skim"
		open theFile
	end tell
end procfile

-- more code from the previous example here

Drewfus, I do have Acrobat Pro but not your menu items are you using version 8? Im also curious as to what the Batch Sequence does?