Passing Variables Between Two AppleScripts

Basically, I need to pass a variable that is an empty list from one AppleScript to a second AppleScript. The second AppleScript needs to add a list of filed paths to that list and pass the variable with the full list back to the first script. I can pass the variable from the first script to the second but am having trouble passing the redefined variable back to the first script.

Here’s the basic idea of what I’m trying to do (There is a lot more code than this):

SCRIPT 1:

on open droppedfiles
	with timeout of 900 seconds
		
		
		--Creating an empty list to add file paths to
		set filed_list_print to {}
		
		
		--Photoshop stuff
		repeat with file_to_move in droppedfiles
			tell application "Adobe Photoshop CC 2017"
				activate
				set display dialogs to never
				open alias file_to_move --showing dialogs never
				--bunch of photoshop processing here...
			end tell
			
			
			--Calling the second script, which files the images
			set print_filing_script to alias "Volumes:NAS:Advertising Department:16_SCRIPTS:Pre-Press Scripts:2. Photoshop:Both Print and Web Images:z_Scripts Called From Other Scripts:zFiling Script_Print.scpt"
			if file_to_move is not in check_image_list then run script print_filing_script with parameters {file_to_move, new_name, filed_list_print}
		end repeat
		

		display dialog filed_list_print as string -->Is empty at this point
		
	end timeout
end open

SCRIPT 2:

on run {file_to_move, new_name, filed_list_print}
	
	
	try
		
		--Storing AG prefixes in list
		set AG00string to {"AG00", "AG01", "AG02", "AG03", "AG04", "AG05", "AG06", "AG07", "AG08", "AG09"}
		
		
		--Moving image to proper folder
		if AG00string contains (text 1 thru 4 of new_name) then
			set print_image_folder to alias "Volumes:NAS:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:AG-high res images:AG00 - AG09 - HI RES:"
			move file_to_move to print_image_folder with replacing
		else if AG10string contains (text 1 thru 4 of new_name) then
			set print_image_folder to alias "Volumes:NAS:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:AG-high res images:AG10 - AG19 - HI RES:"
			move file_to_move to print_image_folder with replacing
		end if
		
		--Moving file to error folder if proper folder wasn't found
	on error
		set print_image_folder to alias "Volumes:NAS:Advertising Department:5_ADV PREPRESS:1_ADV IMAGE PROCESSING:2_PROCESSING:Melissa Processing:Scripts:File Manually in 1_4c IMAGES:"
		move file_to_move to print_image_folder with replacing
		
	end try
	
	
	--Adding the path to the folder in which the image was filed to the filed list
	set filed_list_print to filed_list_print & print_image_folder
	display dialog filed_list_print as string -->Contains the last image path at this point
	return filed_list_print
end run

Hi.

You have to do something to receive the returned list, such as:

 if file_to_move is not in check_image_list then set filed_list_print to (run script print_filing_script with parameters {file_to_move, new_name, filed_list_print})

The list received by the other script appears to be a copy rather than the original, and adding items to it by concatentation produces yet more different lists. So the original list doesn’t get changed.

I was so fixed on the list problem yesterday that I didn’t think to see what else might be wrong or how calling the other script might be better handled.

The called script could have an ordinary handler instead of a run handler and be loaded into the main script as a library. The main script would then call the handler instead of running the script. This would be more efficient and the passed list would be the same list in both scripts.

The efficient way to append an item to a list is to set the end of the list to the item. This does actually append the item to the original list. The concatenation method you used coerces the item to a list (if it isn’t one already) and concatenates the two lists, producing a third list containing the items from the other two.

The ‘move’ command should be sent to some application, presumably the Finder in this case. (But maybe you posted an except from a larger script, leaving out the ‘tell’ statement.)

SCRIPT 1:

on open droppedfiles
	with timeout of 900 seconds
		
		
		--Creating an empty list to add file paths to
		set filed_list_print to {}
		-- Load the filing script as a library, before the repeat.
		set print_filing_script to (load script alias "Volumes:NAS:Advertising Department:16_SCRIPTS:Pre-Press Scripts:2. Photoshop:Both Print and Web Images:z_Scripts Called From Other Scripts:zFiling Script_Print.scpt")
		
		
		--Photoshop stuff
		repeat with file_to_move in droppedfiles
			tell application "Adobe Photoshop CC 2017"
				activate
				set display dialogs to never
				open alias file_to_move --showing dialogs never
               			bunch of photoshop processing here...
			end tell
			
			
			-- Calling the second script's handler, which files the images
			if file_to_move is not in check_image_list then tell print_filing_script to moveAndAppendToList(file_to_move, new_name, filed_list_print)
		end repeat
		
		
		display dialog filed_list_print as string
		
	end timeout
end open

SCRIPT 2:

on moveAndAppendToList(file_to_move, new_name, filed_list_print)
	
	
	try
		
		--Storing AG prefixes in list
		set AG00string to {"AG00", "AG01", "AG02", "AG03", "AG04", "AG05", "AG06", "AG07", "AG08", "AG09"}
		
		
		--Moving image to proper folder
		if AG00string contains (text 1 thru 4 of new_name) then
			set print_image_folder to alias "Volumes:NAS:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:AG-high res images:AG00 - AG09 - HI RES:"
			tell application "Finder" to move file_to_move to print_image_folder with replacing
		else if AG10string contains (text 1 thru 4 of new_name) then
			set print_image_folder to alias "Volumes:NAS:Advertising Department:2_IMAGES - HIGH RES FOR PRINT:1_4c IMAGES:AG-high res images:AG10 - AG19 - HI RES:"
			tell application "Finder" to move file_to_move to print_image_folder with replacing
		end if
		
		--Moving file to error folder if proper folder wasn't found
	on error
		set print_image_folder to alias "Volumes:NAS:Advertising Department:5_ADV PREPRESS:1_ADV IMAGE PROCESSING:2_PROCESSING:Melissa Processing:Scripts:File Manually in 1_4c IMAGES:"
		tell application "Finder" to move file_to_move to print_image_folder with replacing
		
	end try
	
	
	--Adding the path to the folder in which the image was filed to the filed list
	set end of filed_list_print to print_image_folder
	display dialog filed_list_print as text --> Includes the last image path at this point
	
	return filed_list_print -- Optional as it's the same list that was passed in, only with an added item.
end moveAndAppendToList