Building a list

Ok, I appreciate everyone who has been trying to help me out. I have limited experience in scripting or coding, so I know I have a lot of questions that may seem fairly simple.

I’m trying to set up a script where:

1)user is prompted to select files (multiple allowed)
2)get the path and file name of each file and build a list out of that info
3)get the file name w/o extension and without “IMPO”, then build a list of that info
4)user is prompted to select an output folder
5)the first file in the file list is then recalled, opened, and then performs another script on itself
6)file is saved using the first file name in the name list, then closed (but application stays opened)
5&6 are repeated as needed using the corresponding pieces of the two lists
7)once last file is done, the application closes.

here’s all I know so far:

  1. (set list InputList to?) choose file with multiple selections allowed
  2. I don’t know this one
  3. I don’t know how to get the filename, I don’t know what delimiters to use to get rid of the extension and “IMPO”
  4. set OutputFolder to choose folder
  5. I don’t know this one
  6. I don’t know this one
    I imagine it would be a good idea to make my repetitive task into a handler
  7. not sure of this one, all I know is quit

thanks again for everyone’s help!

edit: I wanted to tell everyone two things: I know it’s possible this has been answered in the past, but there are just so many pages (no matter what you search for, it seems) that it takes forever to sift through them (I did for a bit). Also, I don’t want anyone to feel like I want you guys to write my script for me, I just don’t know where to find all the resources I need. I wish there was some place where I could see every single term applescript can use, and how to use it right.

Does anyone know of any place to chat on IRC about applescript?

edit2: I think I almost figured out how to build a list from file names, but I keep getting an error saying can’t get every file alias of…

set Location to choose folder
set InputList to the alias of every file of Location
print InputList

I’m starting to figure some of it out, but I’m sill stuck. Here’s what I have so far.

set InputPDF to (choose file with prompt "Select input PDF" with multiple selections allowed) as string
set OutputFolder to (choose folder with prompt "Choose the destination folder (Modified Originals)") as string
set InputName to "AA"
set PDFname to InputName
tell application "Finder"
	open first file of InputPDF
	activate application "Adobe Acrobat Professional"
	tell application "System Events"
		tell process "Acrobat"
			--1 through 12
			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 & PDFname & "1-12")
			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"
			set InputName to "AB"
			set PDFnam to InputName
			--2 through 11
			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 & PDFname & "1-12")
			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

[I put the script in AppleScript tags, AB]

Hi drewfus,

I tried to help you with point 1 to 4 of your task list by writing custom educational code separated into several subfunctions that might get you started.

I cannot currently help you with point 5 and 6 of your list, as I simply don’t know what you want to do with the files. Moreover I did not understand the «IMPO» part.

Anyway, here is the script code :wink:


on run
	-- 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 outoput 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

Thanks for the help, I’m well on my way now.

“IMPO” is simply four letters that are always in the file names of what I’m manipulating. I would like to remove these letters from the name. As for 5 & 6; 5 is just that I want to open the first file from the InputPDF list. 6 is that the file is saved (using the original file name (-“IMPO”) and the page number (1-16) then closed, yet keeping the application open.

Let me know if I need to explain anything else. Back to working on the script. I’ll post what I’ve done so far in an hour or so.

Thanks, that’s helping a lot. Here is what I have right now. I don’t know how to save to the chosen path and give it the file name I want. I think I’m close to figuring out the saving, but I have to be doing something wrong. “save open document in chosenfolder as (outfilenames & “1-16”)” wont compile. I’m also having trouble getting the file names. It keeps saying unable to get file names, or something like that.

-- 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
	activate application "Adobe Acrobat Professional"
	tell application "System Events"
		       tell process "Acrobat"
			
			--1 through 16 
			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
			save open document in chosenfolder as (outfilenames & "1-16")
			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

			--2 through 15
			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