How to get 2 scripts to work together? Illustrator & Acrobat Pro PDFs

I just started looking into using Applescript.

I’m working on creating a folder that when I drop 1 or 300 Illustrator files into it the script will

1) Open the .ai file in Illustrator save as .pdf with same name. (I have a script that does this see Script #1 below)

2) Then opens the .pdf file in Acrobat Pro & converts pdf to “Enable Commenting & Measuring” keeping the same name (I have a script that does this see Script #2 below)

Note: I do not know how to combined them into one script so the script does this to each file one at a time.

3) Then when 1 & 2 are complete I would like the .ai file & converted .pdf file to be moved to a different folder based on the first 6 characters of its name.

Example:
If a file is named “ILDG93-180101-14401-AA01.ai” then because it starts with “ILDG93” it will know to move both files .ai & .pdf to a folder called “CD4.1”
but then
If a file is named “ILCJ54-180101-14401-AA01.ai” then because it starts with “ILCJ54” it will know to move both files .ai & .pdf to a folder called “C520” & so on…

4) Then moves on to the next .ai file until all are complete.

One more thing I was hoping that if 10 files were dropped into the folder & it was in the middle of processing the 10 .ai files & at that time more files were dropped into the folder it would be able to just keep working on the 10 & continue to the new files until all are done.

:smiley: If anyone knows how to do any of this even if not all I’m asking I would be so THANKFUL!

Script #1 SAVE AS .ai as .pdf

-- fileList is a list of aliases to Illustrator files 
-- destFolder is an alias to a folder where PDF files are saved 
on SaveFilesAsPDF(fileList, destFolder)
	set destPath to destFolder as Unicode text
	repeat with aFile in fileList
		tell application "Finder" to set fileName to name of aFile
		set newFilePath to destPath & fileName
		tell application "Adobe Illustrator"
			open aFile
			save current document in file newFilePath as pdf with options {class:PDF save options, acrobat layers:false, compatibility:Acrobat 8, preserve editability:false, font subset threshold:0.0}
			close current document saving no
		end tell
	end repeat
end SaveFilesAsPDF
-- Call handler 
set sourceFolder to "Macintosh HD:Users:b3po:Public:AI to PDF to Enabled"
tell application "Finder" to set fileList to every file of folder "Macintosh HD:Users:b3po:Public:AI to PDF to Enabled" as alias list
set destFolder to "Macintosh HD:Users:b3po:Public:PDF Enabler:"
SaveFilesAsPDF(fileList, destFolder)

--tell application "Finder" to set theFiles to every document file of folder "Macintosh HD:Users:b3po:Public:AI to PDF to Enabled" whose file type ends with "ai"

Script #2 CONVERT pdf “Enable Commenting & Measuring”

property documentList : {}

on open names
	set documentList to {}
	tell application "Finder"
		repeat with i in names
			set documentList to documentList & ((i as alias) as string)
		end repeat
		repeat with i in documentList -- in case multiple objects dropped on applet
			if i ends with ":" then -- if object is a folder process its contents too
				set newList to entire contents of (i as alias)
				repeat with j in newList
					set documentList to documentList & (j as string)
				end repeat
			end if
		end repeat
	end tell
	my process_documents(documentList)
end open

on process_documents(theList)
	repeat with aDocument in theList
		if (aDocument does not end with ":") then
			tell application "Adobe Reader"
				set myDocument to open (alias aDocument)
			end tell
			tell application "System Events"
				try
					tell process "Acrobat"
						-- bring acrobat to the front
						set frontmost to true
						set windowName to value of static text 1 of window 1
						-- select the enable commenting menu item
						try
							-- Acrobat Pro 9
							click menu item "Enable for Commenting and Analysis in Adobe Reader..." of menu 1 of menu bar item "Comments" of menu bar 1
						on error
							-- Acrobat Pro X
							try
								-- OS X Snow Leopard
								click menu item "Enable Commenting & Measuring..." of menu 1 of menu item "Reader Extended PDF" of menu 1 of menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1
							on error
								-- OS X Lion
								click menu item "Enable Commenting & Measuring..." of menu 1 of menu item "Reader Extended PDF" of menu 1 of menu item "Save As" of menu 1 of menu bar item "File" of menu bar 1
							end try
						end try
						
						-- change save as filename
						-- wait until save dialog is opened
						set saveDialogActive to false
						repeat until saveDialogActive is true
							try
								set fileName to value of text field 1 of window "Save As"
								set saveDialogActive to true
							on error
								set saveDialogActive to false
							end try
						end repeat
						
						set value of text field 1 of window "Save As" to fileName
						-- save file
						click button "Save" of window "Save As"
						if exists sheet 1 of window "Save As" then
							click button "Replace" of sheet 1 of window "Save As"
						end if
					end tell
				end try
			end tell
			
			-- stop processing until window is closed
			set windowActive to true
			repeat until windowActive is false
				tell application "System Events"
					try
						tell process "Acrobat"
							-- bring acrobat to the front
							set frontmost to true
							-- close file
							click button 1 of window windowName
							set windowName to value of static text 1 of window 1
						end tell
					on error
						set windowActive to false
					end try
				end tell
			end repeat
			
		end if
	end repeat
end process_documents

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Model: imac
AppleScript: 2.3
Browser: Safari 534.55.3
Operating System: Mac OS X (10.6)

This works on my Mac. I have Illustrator’s preferences to open a PDF after saving, which you would have to do manually beforehand. All I did was take your second script handler, remove its repeat section and call the handler before Illustrator moved on to opening its next document.

Regarding your last point, I don’t think you would be able to interrupt the script with some more work, Maybe some kind of watched folder thing would do it.

-- fileList is a list of aliases to Illustrator files 
-- destFolder is an alias to a folder where PDF files are saved 
on SaveFilesAsPDF(fileList, destFolder)
	set destPath to destFolder as Unicode text
	repeat with aFile in fileList
		tell application "Finder" to set fileName to name of aFile
		set newFilePath to destPath & fileName
		tell application "Adobe Illustrator"
			open aFile
			save current document in file newFilePath as pdf with options {class:PDF save options, acrobat layers:false, compatibility:Acrobat 8, preserve editability:false, font subset threshold:0.0}
			close current document saving no
		end tell
		my process_documents()
	end repeat
end SaveFilesAsPDF
-- Call handler 
set sourceFolder to "Macintosh HD:Users:Dave:Desktop:Illustrator:"
tell application "Finder" to set fileList to every file of folder sourceFolder -- "Macintosh HD:Users:b3po:Public:AI to PDF to Enabled" as alias list
set destFolder to "Macintosh HD:Users:Dave:Desktop:PDF:"
SaveFilesAsPDF(fileList, destFolder)

--tell application "Finder" to set theFiles to every document file of folder "Macintosh HD:Users:b3po:Public:AI to PDF to Enabled" whose file type ends with "ai"

on process_documents()
	tell application "System Events"
		try
			tell process "Acrobat"
				-- bring acrobat to the front
				set frontmost to true
				set windowName to value of static text 1 of window 1
				-- select the enable commenting menu item
				try
					-- Acrobat Pro 9
					click menu item "Enable for Commenting and Analysis in Adobe Reader..." of menu 1 of menu bar item "Comments" of menu bar 1
				on error
					-- Acrobat Pro X
					try
						-- OS X Snow Leopard
						click menu item "Enable Commenting & Measuring..." of menu 1 of menu item "Reader Extended PDF" of menu 1 of menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1
					on error
						-- OS X Lion
						click menu item "Enable Commenting & Measuring..." of menu 1 of menu item "Reader Extended PDF" of menu 1 of menu item "Save As" of menu 1 of menu bar item "File" of menu bar 1
					end try
				end try
				
				-- change save as filename
				-- wait until save dialog is opened
				set saveDialogActive to false
				repeat until saveDialogActive is true
					try
						set fileName to value of text field 1 of window "Save As"
						set saveDialogActive to true
					on error
						set saveDialogActive to false
					end try
				end repeat
				
				set value of text field 1 of window "Save As" to fileName
				-- save file
				click button "Save" of window "Save As"
				if exists sheet 1 of window "Save As" then
					click button "Replace" of sheet 1 of window "Save As"
				end if
			end tell
		end try
	end tell
	
	-- stop processing until window is closed
	set windowActive to true
	repeat until windowActive is false
		tell application "System Events"
			try
				tell process "Acrobat"
					-- bring acrobat to the front
					set frontmost to true
					-- close file
					click button 1 of window windowName
					set windowName to value of static text 1 of window 1
				end tell
			on error
				set windowActive to false
			end try
		end tell
	end repeat
end process_documents

Model: MacBook Pro
AppleScript: 2.1.2
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

This is a big help! I started to look into creating a watched folder & other ways to put in a script that can move files based on the name of the file. I found some scripts that I will test to see if I can get them to work then see if I can figure out how to merge it into the script you made. If not, I may ask you if you can.

Hopefully this was not to much work & I thank you for your help!!

You’re welcome “ let us know how you go.

I really tried! Over & Over again!:mad:
I have 2 options that move files base on the name but am not sure how to connect it all together? Do you have any idea?

OPTION 1:

A program called “HAZEL” cost $25 & my well be worth it.

If in the script we could add a simple move command to move the .ai file & .pdf file when finished to an “Other Folder” then I could with HAZEL have that “Other Folder” watched to push all the files to their correct final folder based on their name.

So it would:

one at a time open each .ai file in Illustrator then “save as” a .pdf then open the .pdf in Acrobat Pro to make “Enable Commenting & Measuring…” Then moves both completed files to an other folder & then repeats to the next .ai until all .ai’s & .pdf’s are processed & moved out to the next folder

Would it be easy to add this? I’m thinking not to hard, but what do I know. I am going to try to do it but am always scared to death when scripting, I always wonder if I forgot one little detail or am totally off.

OPTION 2:

This could work if I could get it to run the conversion script 1st then move both .ai & new .pdf one file at a time.

But maybe the HAZEL option is better because it can do more than just move files based on their name? Just a guess.

tell application "Finder"
	set this_folder to folder "Macintosh HD:Users:brianlau:Desktop:Drop pdf's"
	set this_list to every file of this_folder
	repeat with i in this_list
		if (name of i) begins with "ILCJ54" then
			move i to "Macintosh HD:Users:brianlau:Desktop:Completed Pages:C520-13 CJ54:" with replacing
			
		else if (name of i) begins with "ILDM51" then
			move i to "Macintosh HD:Users:brianlau:Desktop:Completed Pages:C346-13 DM51:" with replacing
			
		else if (name of i) begins with "ILDE83" then
			move i to "Macintosh HD:Users:brianlau:Desktop:Completed Pages:B299-14 DE83:" with replacing
			
		else if (name of i) begins with "ILEJ73" then
			move i to "Macintosh HD:Users:brianlau:Desktop:Completed Pages:C489-14 EJ73:" with replacing
			
		else if (name of i) begins with "ILFT43" then
			move i to "Macintosh HD:Users:brianlau:Desktop:Completed Pages:CD4.2-15 FT43:" with replacing
			
		end if
	end repeat
end tell

Model: imac
AppleScript: 2.3
Browser: Safari 534.55.3
Operating System: Mac OS X (10.6)

I think this is what you are after. I have reorganised the order of the handlers so that they come after the main code. It doesn’t make any difference to how they work, I just find it easier to read that way.


set sourceFolder to "Macintosh HD:Users:Dave:Desktop:Illustrator:"
tell application "Finder" to set fileList to every file of folder sourceFolder -- "Macintosh HD:Users:b3po:Public:AI to PDF to Enabled" as alias list
set destFolder to "Macintosh HD:Users:Dave:Desktop:PDF:"
SaveFilesAsPDF(fileList, destFolder)

--tell application "Finder" to set theFiles to every document file of folder "Macintosh HD:Users:b3po:Public:AI to PDF to Enabled" whose file type ends with "ai"


-- handler for opening and saving Illustrator files as PDFs
-- fileList is a list of aliases to Illustrator files 
-- destFolder is an alias to a folder where PDF files are saved 
on SaveFilesAsPDF(fileList, destFolder)
	set destPath to destFolder as Unicode text
	repeat with aFile in fileList
		tell application "Finder" to set fileName to name of aFile
		set fileName to remove_extension(fileName) -- get rid of the .ai using the handler at the bottom
		set newFilePath to destPath & fileName & ".pdf" -- add the pdf extension here so that we can capture the filepath for later
		tell application "Adobe Illustrator"
			open aFile as alias without dialogs
			set myPDF to save current document in file newFilePath as pdf with options {class:PDF save options, acrobat layers:false, compatibility:Acrobat 8, preserve editability:false, font subset threshold:0.0}
			log "aFile: " & aFile
			log "newFilePath: " & newFilePath
			close current document saving no
		end tell
		my process_documents()
		tell application "Finder"
			move aFile to "Macintosh HD:Users:Dave:Desktop:Other_Folder:"
			move newFilePath to "Macintosh HD:Users:Dave:Desktop:Other_Folder:"
		end tell
	end repeat
end SaveFilesAsPDF

-- handler for processing PDFs
on process_documents()
	tell application "System Events"
		try
			tell process "Acrobat"
				-- bring acrobat to the front
				set frontmost to true
				set windowName to value of static text 1 of window 1
				-- select the enable commenting menu item
				try
					-- Acrobat Pro 9
					click menu item "Enable for Commenting and Analysis in Adobe Reader..." of menu 1 of menu bar item "Comments" of menu bar 1
				on error
					-- Acrobat Pro X
					try
						-- OS X Snow Leopard
						click menu item "Enable Commenting & Measuring..." of menu 1 of menu item "Reader Extended PDF" of menu 1 of menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1
					on error
						-- OS X Lion
						click menu item "Enable Commenting & Measuring..." of menu 1 of menu item "Reader Extended PDF" of menu 1 of menu item "Save As" of menu 1 of menu bar item "File" of menu bar 1
					end try
				end try
				
				-- change save as filename
				-- wait until save dialog is opened
				set saveDialogActive to false
				repeat until saveDialogActive is true
					try
						set fileName to value of text field 1 of window "Save As"
						set saveDialogActive to true
					on error
						set saveDialogActive to false
					end try
				end repeat
				
				set value of text field 1 of window "Save As" to fileName
				-- save file
				click button "Save" of window "Save As"
				if exists sheet 1 of window "Save As" then
					click button "Replace" of sheet 1 of window "Save As"
				end if
			end tell
		end try
	end tell
	
	-- stop processing until window is closed
	set windowActive to true
	repeat until windowActive is false
		tell application "System Events"
			try
				tell process "Acrobat"
					-- bring acrobat to the front
					set frontmost to true
					-- close file
					click button 1 of window windowName
					set windowName to value of static text 1 of window 1
				end tell
			on error
				set windowActive to false
			end try
		end tell
	end repeat
end process_documents

on remove_extension(this_name)
	if this_name contains "." then
		set oldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {"."}
		set this_name to text 1 thru text item -2 of this_name
		set AppleScript's text item delimiters to oldDelims
	end if
	return this_name
end remove_extension

Model: MacBook Pro
AppleScript: 2.1.2
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

I tried & I tried & now have it working with hazel. YOU ARE AWESOME! THANKS.

I do have one problem though.

The folder that the .ai files are dropped into is a folder that can be connected to by several people. So the issue is if I dropped 10 files into the folder & the script is running & while the script is processing the 10 files someone else drops 5 other files into the folder the script will keep running but it does not process the 5 newly added files unless when the script is not running then someone adds one or more files.

Is this an easy addition?

If you can add this then this will convince my boss to hire a real AppleScripter to work with me to create an ever better more important process I have in mind. PLEASE let me know if you are interested in this job opportunity & how much you would charge. I have been at this company for 16 yrs we mainly do illustration work for FORD. Our company is T.I.C.

Look forward to maybe work with you.

http://www.ticglobal.com/

Brian Lau
T I C
Methods & Procedures Coordinator

(313) 982-9660 Ext. 306

It might work if the script was saved as an application and then more than one user could drop files on it. Don’t know but will give it a go.

Model: MacBook Pro
AppleScript: 2.1.2
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)