Combining two Applescripts

Hi there,

I’m stuck with a little problem while trying to merge the two scripts into one. Both the Scripts (1 & 2) are working fine individually, however, when combining these two, the try commands are not executing.

Can you please look at the script below and help me solve the problem.

Thanks in advance.

Final AppleScript (merged)

with timeout of 86400 seconds
	
	tell application "Finder"
		set pNum to text returned of (display dialog "Enter Even Page No. : " default answer "")
		
		tell application "Adobe InDesign CS5"
			set myDocument to active document
			set docName to name of myDocument
			set sourceName to text 1 thru -6 of docName
			set sourcePath to the file path of myDocument as string
			
			tell application "Finder"
				set packFolder to make new folder at sourcePath with properties {name:sourceName}
			end tell
			
			tell active document
				save myDocument
				package to alias (packFolder as string) copying fonts no copying linked graphics yes including hidden layers yes copying profiles no updating graphics yes ignore preflight errors yes creating report no
				close myDocument
			end tell
		end tell
		
		tell application "Finder"
			
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {"."}
			set FullName to (packFolder as string) & "_" & pNum
			
			make new folder at packFolder with properties {name:"BL"}
			make new folder at packFolder with properties {name:"BL_RNT"}
			set CTf to make new folder at packFolder with properties {name:"CT"}
			set DFf to make new folder at packFolder with properties {name:"DF"}
			make new folder at packFolder with properties {name:"MZ"}
			set PDFf to make new folder at packFolder with properties {name:"PDF"}
			make new folder at packFolder with properties {name:"INDD"}
			make new folder at packFolder with properties {name:"INDD_RNT"}
			make new folder at packFolder with properties {name:"TAB"}
			make new folder at packFolder with properties {name:"TAB_RNT"}
			
			try
				if exists (1st file of folder "Links" of folder sourcePath whose name contains sourceName & ".jpg") then
					move (1st file of folder "Links" of folder sourcePath whose name contains sourceName & ".jpg") to sourcePath with replacing
				end if
				
				if exists (1st file of folder packFolder whose name extension is "indd") then set name of 1st file of folder packFolder whose name extension is "indd" to FullName & ".indd"
				if exists (1st file of folder sourcePath whose name extension is "pdf") then set name of 1st file of folder sourcePath whose name extension is "pdf" to FullName & ".pdf"
				if exists (1st file of folder sourcePath whose name extension is "jpg") then set name of 1st file of folder sourcePath whose name contains sourceName & ".jpg" to FullName & ".jpg"
				
				if exists (1st file of folder packFolder whose name is FullName & ".indd") then move (1st file of folder packFolder whose name is FullName & ".indd") to DFf
				if exists (1st file of folder sourcePath whose name is FullName & ".pdf") then move (1st file of folder sourcePath whose name is FullName & ".pdf") to PDFf
				if exists (1st file of folder sourcePath whose name is FullName & ".jpg") then move (1st file of folder sourcePath whose name is FullName & ".jpg") to CTf
				move (every file of folder "Links" of folder sourcePath) to packFolder
				if (exists folder "Links" of folder sourcePath) then delete folder "Links" of folder sourcePath
				
				set AppleScript's text item delimiters to oldDelims
			end try
		end tell
	end tell
end timeout

AppleScript 1 (Create Package)

with timeout of 86400 seconds
	
	tell application "Adobe InDesign CS5"
		
		set myDocument to active document
		set docName to name of myDocument
		set sourceName to text 1 thru -6 of docName
		set sourcePath to the file path of myDocument as string
		
		tell application "Finder"
			set packFolder to make new folder at sourcePath with properties {name:sourceName & "_Package"}
			set pdfFolder to make new folder at packFolder with properties {name:"PDF"}
		end tell
		
		tell active document
			save myDocument
			package to alias (packFolder as string) copying fonts yes copying linked graphics yes including hidden layers yes copying profiles no updating graphics yes ignore preflight errors yes creating report no
			close myDocument
		end tell
		
	end tell
	
end timeout

AppleScript 2 (Folder structure for the Package with renaming)

tell application "Finder"
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"."}
	
	set theLocation to (target of front window) as string
	set Nf to text returned of (display dialog "Enter the name of the Folder : " & return & "i.e. name of the Image folder" default answer "")
	set NName to text returned of (display dialog "Enter Even Page No. : " default answer "")
	set FullName to Nf & "_" & NName
	
	set Nf to make new folder at theLocation with properties {name:Nf}
	make new folder at Nf with properties {name:"BL"}
	make new folder at Nf with properties {name:"BL_RNT"}
	set CTf to make new folder at Nf with properties {name:"CT"}
	set DFf to make new folder at Nf with properties {name:"DF"}
	make new folder at Nf with properties {name:"MZ"}
	set PDFf to make new folder at Nf with properties {name:"PDF"}
	make new folder at Nf with properties {name:"INDD"}
	make new folder at Nf with properties {name:"INDD_RNT"}
	make new folder at Nf with properties {name:"TAB"}
	make new folder at Nf with properties {name:"TAB_RNT"}
	if exists (every file of folder theLocation whose name contains "Instructions" & ".txt") then delete (every file of folder theLocation whose name contains "Instructions" & ".txt")
	
	try
		set InDn to (1st file of folder theLocation whose name extension is "indd")
		set FN to name of InDn
		set nwe to first text item of FN
		
		if exists (1st file of folder "Links" of folder theLocation whose name contains nwe & ".jpg") then
			move (1st file of folder "Links" of folder theLocation whose name contains nwe & ".jpg") to theLocation with replacing
		end if
		
		if exists (1st file of folder theLocation whose name extension is "indd") then set name of 1st file of folder theLocation whose name extension is "indd" to FullName & ".indd"
		if exists (1st file of folder theLocation whose name extension is "pdf") then set name of 1st file of folder theLocation whose name extension is "pdf" to FullName & ".pdf"
		if exists (1st file of folder theLocation whose name extension is "jpg") then set name of 1st file of folder theLocation whose name contains nwe & ".jpg" to FullName & ".jpg"
		
		if exists (1st file of folder theLocation whose name is FullName & ".indd") then move (1st file of folder theLocation whose name is FullName & ".indd") to DFf
		if exists (1st file of folder theLocation whose name is FullName & ".pdf") then move (1st file of folder theLocation whose name is FullName & ".pdf") to PDFf
		if exists (1st file of folder theLocation whose name is FullName & ".jpg") then move (1st file of folder theLocation whose name is FullName & ".jpg") to CTf
		move (every file of folder "Links" of folder theLocation) to Nf
		if (exists folder "Links" of folder theLocation) then delete folder "Links" of folder theLocation
		if (exists folder "Document fonts" of folder theLocation) then delete folder "Document fonts" of folder theLocation
		
		set AppleScript's text item delimiters to oldDelims
	end try
end tell

Can someone help me correct the “Try” commands…

As I don’t own inDesign I tried to mimic its behavior running the script below.

with timeout of 86400 seconds
	set pNum to text returned of (display dialog "Enter Even Page No. : " default answer "")
	(*
	tell application "Adobe InDesign CS5"
		
		set myDocument to active document
		set docName to name of myDocument
		set sourceName to text 1 thru -6 of docName
		set sourcePath to the file path of myDocument as string
		*)
	set sourcePath to (path to desktop as text) & "dossier sans titre"
	set sourceName to "une image"
	tell application "Finder"
		set packFolder to (make new folder at sourcePath with properties {name:sourceName & "_Package"}) as alias
		--set pdfFolder to (make new folder at packFolder with properties {name:"PDF"}) as alias
	end tell
	(*
		tell active document
			save myDocument
			package to alias (packFolder as string) copying fonts yes copying linked graphics yes including hidden layers yes copying profiles no updating graphics yes ignore preflight errors yes creating report no
			close myDocument
		end tell
		
	end tell
	*)
	
	tell application "Finder"
		duplicate file ((path to desktop as text) & "my image.indd") to packFolder
		
		duplicate file ((path to desktop as text) & "my image.indd") to folder sourcePath
	end tell
	
end timeout


tell application "Finder"
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"."}
	--set FullName to (packFolder as string) & "_" & pNum # WRONG INSTRUCTION
	set FullName to (name of packFolder as string) & "_" & pNum # CORRECT ONE
	set theLocation to sourcePath
	set Nf to packFolder
	make new folder at Nf with properties {name:"BL"}
	make new folder at Nf with properties {name:"BL_RNT"}
	set CTf to make new folder at Nf with properties {name:"CT"}
	set DFf to make new folder at Nf with properties {name:"DF"}
	make new folder at Nf with properties {name:"MZ"}
	set PDFf to make new folder at Nf with properties {name:"PDF"}
	make new folder at Nf with properties {name:"INDD"}
	make new folder at Nf with properties {name:"INDD_RNT"}
	make new folder at Nf with properties {name:"TAB"}
	make new folder at Nf with properties {name:"TAB_RNT"}
	if exists (every file of folder theLocation whose name contains "Instructions" & ".txt") then delete (every file of folder theLocation whose name contains "Instructions" & ".txt")
	
	try
		set InDn to (1st file of folder theLocation whose name extension is "indd")
		set FN to name of InDn
		set nwe to first text item of FN
		
		if exists (1st file of folder "Links" of folder theLocation whose name contains nwe & ".jpg") then
			move (1st file of folder "Links" of folder theLocation whose name contains nwe & ".jpg") to theLocation with replacing
		end if
		
		if exists (1st file of folder theLocation whose name extension is "indd") then set name of 1st file of folder theLocation whose name extension is "indd" to FullName & ".indd"
		if exists (1st file of folder theLocation whose name extension is "pdf") then set name of 1st file of folder theLocation whose name extension is "pdf" to FullName & ".pdf"
		if exists (1st file of folder theLocation whose name extension is "jpg") then set name of 1st file of folder theLocation whose name contains nwe & ".jpg" to FullName & ".jpg"
		
		if exists (1st file of folder theLocation whose name is FullName & ".indd") then move (1st file of folder theLocation whose name is FullName & ".indd") to DFf
		if exists (1st file of folder theLocation whose name is FullName & ".pdf") then move (1st file of folder theLocation whose name is FullName & ".pdf") to PDFf
		if exists (1st file of folder theLocation whose name is FullName & ".jpg") then move (1st file of folder theLocation whose name is FullName & ".jpg") to CTf
		move (every file of folder "Links" of folder theLocation) to Nf
		if (exists folder "Links" of folder theLocation) then delete folder "Links" of folder theLocation
		if (exists folder "Document fonts" of folder theLocation) then delete folder "Document fonts" of folder theLocation
		
		set AppleScript's text item delimiters to oldDelims
	end try
end tell


It worked.

So, I assume that the code below will work too.

with timeout of 86400 seconds
	set pNum to text returned of (display dialog "Enter Even Page No. : " default answer "")
	
	tell application "Adobe InDesign CS5"
		
		set myDocument to active document
		set docName to name of myDocument
		set sourceName to text 1 thru -6 of docName
		set sourcePath to the file path of myDocument as string
		
	
	tell application "Finder"
		set packFolder to (make new folder at sourcePath with properties {name:sourceName & "_Package"}) as alias
		--set pdfFolder to (make new folder at packFolder with properties {name:"PDF"}) as alias
	end tell
	
		tell active document
			save myDocument
			package to alias (packFolder as string) copying fonts yes copying linked graphics yes including hidden layers yes copying profiles no updating graphics yes ignore preflight errors yes creating report no
			close myDocument
		end tell
		
	end tell
	
	
end timeout


tell application "Finder"
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"."}
	--set FullName to (packFolder as string) & "_" & pNum # WRONG INSTRUCTION
	set FullName to (name of packFolder as string) & "_" & pNum # CORRECT ONE
	set theLocation to sourcePath # a string object
	set Nf to packFolder # an alias object
	make new folder at Nf with properties {name:"BL"}
	make new folder at Nf with properties {name:"BL_RNT"}
	set CTf to make new folder at Nf with properties {name:"CT"}
	set DFf to make new folder at Nf with properties {name:"DF"}
	make new folder at Nf with properties {name:"MZ"}
	set PDFf to make new folder at Nf with properties {name:"PDF"}
	make new folder at Nf with properties {name:"INDD"}
	make new folder at Nf with properties {name:"INDD_RNT"}
	make new folder at Nf with properties {name:"TAB"}
	make new folder at Nf with properties {name:"TAB_RNT"}
	if exists (every file of folder theLocation whose name contains "Instructions" & ".txt") then delete (every file of folder theLocation whose name contains "Instructions" & ".txt")
	
	try
		set InDn to (1st file of folder theLocation whose name extension is "indd")
		set FN to name of InDn
		set nwe to first text item of FN
		
		if exists (1st file of folder "Links" of folder theLocation whose name contains nwe & ".jpg") then
			move (1st file of folder "Links" of folder theLocation whose name contains nwe & ".jpg") to theLocation with replacing
		end if
		
		if exists (1st file of folder theLocation whose name extension is "indd") then set name of 1st file of folder theLocation whose name extension is "indd" to FullName & ".indd"
		if exists (1st file of folder theLocation whose name extension is "pdf") then set name of 1st file of folder theLocation whose name extension is "pdf" to FullName & ".pdf"
		if exists (1st file of folder theLocation whose name extension is "jpg") then set name of 1st file of folder theLocation whose name contains nwe & ".jpg" to FullName & ".jpg"
		
		if exists (1st file of folder theLocation whose name is FullName & ".indd") then move (1st file of folder theLocation whose name is FullName & ".indd") to DFf
		if exists (1st file of folder theLocation whose name is FullName & ".pdf") then move (1st file of folder theLocation whose name is FullName & ".pdf") to PDFf
		if exists (1st file of folder theLocation whose name is FullName & ".jpg") then move (1st file of folder theLocation whose name is FullName & ".jpg") to CTf
		move (every file of folder "Links" of folder theLocation) to Nf
		if (exists folder "Links" of folder theLocation) then delete folder "Links" of folder theLocation
		if (exists folder "Document fonts" of folder theLocation) then delete folder "Document fonts" of folder theLocation
		
		set AppleScript's text item delimiters to oldDelims
	end try
end tell


If it doesn’t, I guess that the easy way to identify the problem is to disable the try and end try instructions so that the wrongdoer clearly claims which it is.

I added comments to describe the class of two objects.

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) lundi 25 juillet 2016 17:18:56

Hi, Masoo. A problem appears to be that the if considerations are trying to test folder packfolder, which is already a folder reference”the specifier is fatally redundant. Disparate application commands should also not be grouped within the same tell block. The dialog and delimiter commands have no relation to the Finder and should not be within its blocks. The mydocument references are unneccesary; properly directing the block to document 1 would increase legibility. There may be more issues than I had time to find. I advise that you read the event log and replies.

Hi Yvan / Marc,

Thanks for your suggestions, I’ll try these out today and get back to you with further questions.
Sorry for responding so late, I was busy somewhere else.

By profession, I’m not neither a script writer nor a programmer. I just try my hands on these to make things easier. In my troubleshooting MacScripter has played a great role and I express my gratitude to all its members.

I’m looking for someone who can teach me JavaScript and AppleScript, wish me luck.

Thanks,
Masood

It worked, thanks Yvan. I made some changes to the script. Instead of creating a folder on the source location, the script is now creating a folder on the Desktop. Now I do not have to move the Package folder from the source to my Desktop. but…

with timeout of 86400 seconds
	set jobFolder to text returned of (display dialog "Enter the name of the Folder : " & return & "i.e. name of the Image folder" default answer "")
	set pageNumber to text returned of (display dialog "Enter Even Page No. : " default answer "")
	set FullName to jobFolder & "_" & pageNumber
	
	tell application "Adobe InDesign CS5"
		
		set myDocument to active document
		set docName to name of myDocument
		set sourceName to text 1 thru -6 of docName
		set sourcePath to the file path of myDocument as string
		
		tell application "Finder"
			set packFolder to (make new folder at desktop with properties {name:jobFolder}) as alias
		end tell
		
		tell active document
			save myDocument
			package to alias (packFolder as string) copying fonts no copying linked graphics yes including hidden layers yes copying profiles no updating graphics yes ignore preflight errors yes creating report no
			save myDocument
			close myDocument
		end tell
		
	end tell
end timeout


tell application "Finder"
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"."}
	set theLocation to sourcePath
	set Nf to packFolder
	
	make new folder at Nf with properties {name:"BL"}
	make new folder at Nf with properties {name:"BL_RNT"}
	set CTf to make new folder at Nf with properties {name:"CT"}
	set DFf to make new folder at Nf with properties {name:"DF"}
	make new folder at Nf with properties {name:"MZ"}
	set PDFf to make new folder at Nf with properties {name:"PDF"}
	make new folder at Nf with properties {name:"INDD"}
	make new folder at Nf with properties {name:"INDD_RNT"}
	make new folder at Nf with properties {name:"TAB"}
	make new folder at Nf with properties {name:"TAB_RNT"}
	
	try
		set InDn to (1st file of folder Nf whose name extension is "indd")
		set FN to name of InDn
		set nwe to first text item of FN
		
		if exists (1st file of folder "Links" of folder Nf whose name contains nwe & ".jpg") then
			move (1st file of folder "Links" of folder Nf whose name contains nwe & ".jpg") to Nf with replacing
		end if
		
		if exists (1st file of folder Nf whose name extension is "indd") then set name of 1st file of folder Nf whose name extension is "indd" to FullName & ".indd"
		if exists (1st file of folder Nf whose name extension is "jpg") then set name of 1st file of folder Nf whose name contains nwe & ".jpg" to FullName & ".jpg"
		if exists (1st file of folder theLocation whose name extension is "pdf") then set name of 1st file of folder theLocation whose name extension is "pdf" to FullName & ".pdf"
		
		if exists (1st file of folder Nf whose name is FullName & ".indd") then move (1st file of folder Nf whose name is FullName & ".indd") to DFf
		if exists (1st file of folder Nf whose name is FullName & ".jpg") then move (1st file of folder Nf whose name is FullName & ".jpg") to CTf
		if exists (1st file of folder theLocation whose name is FullName & ".pdf") then move (1st file of folder theLocation whose name is FullName & ".pdf") to PDFf
		
		move (every file of folder "Links" of folder Nf) to Nf
		if (exists folder "Links" of folder Nf) then delete folder "Links" of folder Nf
		
		set AppleScript's text item delimiters to oldDelims
	end try
end tell

However, when I tried to update the script so that it will see for a folder called “Final_Packages” on the Desktop and create one if unavailable and then create the package inside the “Final_Packages” folder on the Desktop.

This updated script runs perfectly first time (means if there is no “Final_Packages” folder on the Desktop). However, fails the second time (with a “Final_Packages” folder already on the Desktop).

with timeout of 86400 seconds
	set jobFolder to text returned of (display dialog "Enter the name of the Folder : " & return & "i.e. name of the Image folder" default answer "")
	set pageNumber to text returned of (display dialog "Enter Even Page No. : " default answer "")
	set FullName to jobFolder & "_" & pageNumber
	
	tell application "Adobe InDesign CS5"
		
		set myDocument to active document
		set docName to name of myDocument
		set sourceName to text 1 thru -6 of docName
		set sourcePath to the file path of myDocument as string
		
		tell application "Finder"
			if not (exists folder "Final_Packages") of desktop then set myFolder to make new folder at desktop with properties {name:"Final_Packages"}
			set packFolder to (make new folder at myFolder with properties {name:jobFolder}) as alias
		end tell
		
		tell active document
			save myDocument
			package to alias (packFolder as string) copying fonts no copying linked graphics yes including hidden layers yes copying profiles no updating graphics yes ignore preflight errors yes creating report no
			save myDocument
			close myDocument
		end tell
		
	end tell
end timeout

Rest of the script portion is the same as in the original script.

This is the error, when the above script is run second time:

Can you please advise further.

It’s simple, when there is already a folder named Final_Packages, the script doesn’t define the variable myFolder;

replace

     tell application "Finder"
           if not (exists folder "Final_Packages") of desktop then set myFolder to make new folder at desktop with properties {name:"Final_Packages"}
           set packFolder to (make new folder at myFolder with properties {name:jobFolder}) as alias
       end tell

by

tell application "Finder"
	if not (exists folder "Final_Packages") of desktop then
		set myFolder to make new folder at desktop with properties {name:"Final_Packages"}
	else
		tell me to set myFolder to (path to desktop as text) & "Final_Packages"
		set myFolder to myFolder as alias
	end if
	set packFolder to (make new folder at myFolder with properties {name:jobFolder}) as alias
end tell

Here is an enhanced one which takes also care of a possible existing packFolder.

set myFolder to (path to desktop as text) & "Final_Packages"
tell application "Finder"
	if not (exists folder myFolder) then
		make new folder at desktop with properties {name:"Final_Packages"}
	end if
	set packFolder to myFolder & ":" & jobFolder
	set myFolder to myFolder as alias
	if not (exists folder packFolder) then
		make new folder at myFolder with properties {name:jobFolder}
	end if
	set packFolder to packFolder as alias
end tell

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) vendredi 29 juillet 2016 16:25:47

Great, thanks a lot. I’ll test this on Monday. Till then have a great weekend. Take care.

bye :slight_smile: