Array not working when used with "on adding folder items to" function

I’m having issues with an array that I’ve built. It is an AppleScript used as a Folder Action Script, so it runs when files are placed into the specified folder. It uses ExifTool to pull some metadata from the file into a string. The array parses that string into its different segments and assigns that data to specific variables.

The action runs as expected when I input the filename and path manually, but when I pull that info programmatically from the dropped files it chokes. I have confirmed that it is pulling the right information into those variables so am unsure where the issue might be. Anyone have any thoughts. Full code below.

on adding folder items to this_folder after receiving these_items
	set userName to short user name of (system info) as text
	set userMoveToServerFolder to "Volumes:Backup:Move_To_Server:Selects:" as alias
	
	global modelName
	global bayNumber
	global timeOfDay
	global shootFolderName
	global arrayResults
	global studioLocation
	global bscFolder
	global bscName
	global fileName
	global theFileNamePath
	global metadataArray
	global shootFolder
	
	-----------------------
	-----------------------
	-- Adjust metadata and move to server/local backup
	
	set exiftool_path to "/usr/local/bin/"
	set alphaDirectoryPath to "Volumes:extensis:Product_Holding:"
	set alphaDirectoryPathPOSIX to quoted form of POSIX path of alphaDirectoryPath
	set inProgressPath to "Volumes:BACKUP:In_Progress:"
	set inProgressPathPOSIX to quoted form of POSIX path of inProgressPath
	set fileTypeList to {".jpg", ".psd", ".tif", ".mov", ".eip"}
	
	repeat with i from 1 to number of items in these_items
		set this_item to item i of these_items
		set thePath to quoted form of POSIX path of this_item
		tell application "Finder"
			set theFileNamePath to POSIX path of this_item
			set fileName to name of (POSIX file theFileNamePath as alias)
		end tell
		set bscName to text 1 thru 15 of fileName
		
		if fileName ends with ".jpg" then
			
			set metadataArray to do shell script exiftool_path & "exiftool -T -s3 -modelName -bayNumber -timeOfDay -PhotoShootName " & thePath
			
			--set myArray to my theSplit(metadataArray, tab)
			
			my theSplit(metadataArray, tab)
			
		end if
		
		
	end repeat
	
end adding folder items to


on theSplit(theString, theDelimiter)
	-- save delimiters to restore old settings
	set oldDelimiters to AppleScript's text item delimiters
	-- set delimiters to delimiter to be used
	set AppleScript's text item delimiters to theDelimiter
	-- create the array
	set theArray to every text item of theString
	-- restore the old setting
	set AppleScript's text item delimiters to oldDelimiters
	-- return the result
	
	set modelName to text item 1 of theArray
	set timeOfDay to text item 2 of theArray
	set bayNumber to text item 3 of theArray
	set shotFolderName to text item 4 of theArray
end theSplit


And I suppose I should note that the “set metadataArray…” string returns something like this: “flavianamatata B9 AM5 17-05-22 flavianamatata AM5 B9 sonjasaxe W” which is parsed into the different variables
modelName = flavianamatata
timeOfDay = AM5
bayNumber = B9
shootFolderName = 17-05-22 flavianamatata AM5 B9 sonjasaxe W

Hi. I just quickly skimmed your code, so it’s possible there are additional problems, but, if I were to define globals, I would never place them within a subroutine, where their affect would be restricted to the handler instance. Finder also has no idea what a POSIX path/file is, as those are standard additions commands; only name should be directed to Finder.

At first I made some changes in the handler theSplit.

on adding folder items to this_folder after receiving these_items
	-- set userName to short user name of (system info) as text # not used
	-- set userMoveToServerFolder to "Volumes:Backup:Move_To_Server:Selects:" as alias # not used
	(*
	# These globals are useless
	global modelName
	global bayNumber
	global timeOfDay
	global shootFolderName
	global arrayResults
	global studioLocation
	global bscFolder
	global bscName
	global fileName
	global theFileNamePath
	global metadataArray
	global shootFolder
	*)
	-----------------------
	-----------------------
	-- Adjust metadata and move to server/local backup
	
	set exiftool_path to "/usr/local/bin/"
	set alphaDirectoryPath to "Volumes:extensis:Product_Holding:"
	set alphaDirectoryPathPOSIX to quoted form of POSIX path of alphaDirectoryPath
	set inProgressPath to "Volumes:BACKUP:In_Progress:"
	set inProgressPathPOSIX to quoted form of POSIX path of inProgressPath
	set fileTypeList to {".jpg", ".psd", ".tif", ".mov", ".eip"}
	
	repeat with i from 1 to number of items in these_items
		set this_item to item i of these_items
		set thePath to quoted form of POSIX path of this_item
		tell application "Finder"
			set theFileNamePath to POSIX path of this_item
			set fileName to name of (POSIX file theFileNamePath as alias)
		end tell
		set bscName to text 1 thru 15 of fileName
		
		if fileName ends with ".jpg" then
			
			set metadataArray to do shell script exiftool_path & "exiftool -T -s3 -modelName -bayNumber -timeOfDay -PhotoShootName " & thePath
			
			--set myArray to my theSplit(metadataArray, tab)
			
			set {modelName, timeOfDay, bayNumber, shootFolderName} to my theSplit(metadataArray, tab)
			
		end if
		
		
	end repeat
	
end adding folder items to


on theSplit(theString, theDelimiter)
	-- save delimiters to restore old settings
	set oldDelimiters to AppleScript's text item delimiters
	-- set delimiters to delimiter to be used
	set AppleScript's text item delimiters to theDelimiter
	-- create the array
	set theArray to every text item of theString
	-- restore the old setting
	set AppleScript's text item delimiters to oldDelimiters
	-- return the result
	
	(*
	set modelName to text item 1 of theArray
	set timeOfDay to text item 2 of theArray
	set bayNumber to text item 3 of theArray
	set shotFolderName to text item 4 of theArray # there is a typo in the variable name
	*)
	return items 1 thru 4 of theArray
end theSplit


More, I’m puzzled by the way you define several paths.
Just an example.
You use :

set alphaDirectoryPath to "Volumes:extensis:Product_Holding:"

Must I understand that you have an external device named “Volumes” or an external one named “extensis” ?
I ask because “Volumes” is the first component used by macOS in POSIX paths of external devices.
If you have an external device named “extensis”, the correct syntax would be :

set alphaDirectoryPath to "extensis:Product_Holdings:"
set alphaDirectoryPathPOSIX to quoted form of POSIX path of alphaDirectoryPath
--> "'/Volumes/extensis/Product_Holdings/'"

There is ALWAYS a folder named “Volumes” at the root of the boot volume but :

(a) Finder doesn’t see it.

tell application "Finder"
	exists folder "Volumes"
	--> false
end tell]
tell application "System Events"
	exists folder "Volumes"
	--> true
end tell

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 25 mai 2017 10:37:06

Hi.

There’s much in your script that’s incorrect, apparently superfluous, or apparently laborious; but it’s also obviously incomplete, so one hesitates to offer corrected code in case this causes problems in the unposted part of the script.

In addition to what Marc’s pointed out (and I see Yvan’s also mentioned some of the points below):

  1. HFS paths (the type with colons) begin with the volume names, not with “Volumes:”.
  2. In your theSplit() handler:
    i) The items of a list are items, not text items. Text items are what you extract from the text to get the list.
    ii) It would be better to have the handler return the list (aka theArray) and to set the variables to its individual items in the main handler. That way you don’t need the globals. It’s easy to do like this:

			set {modelName, timeOfDay, bayNumber, shotFolderName} to theSplit(metadataArray, tab)
			
		end if
		
		
	end repeat
	
end adding folder items to


on theSplit(theString, theDelimiter)
	-- save delimiters to restore old settings
	set oldDelimiters to AppleScript's text item delimiters
	-- set delimiters to delimiter to be used
	set AppleScript's text item delimiters to theDelimiter
	-- create the array
	set theArray to every text item of theString
	-- restore the old setting
	set AppleScript's text item delimiters to oldDelimiters
	-- return the result
	
	return theArray
end theSplit

iii) Does exiftool return its results in the same order as the options specified in its command line? (I don’t know. I don’t have it. But the note at the end of your post suggests that it does.) If it does, your middle two variables are being set to the wrong values.
3. In the folder action handler, if you only need this_item’s name to see if it ends with “.jpg”, you can dispense with the Finder and simply test if the path ends with “.jpg”:

repeat with i from 1 to number of items in these_items
	set this_item to item i of these_items
	set thePath to POSIX path of this_item
	
	if thePath ends with ".jpg" then
		
		set metadataArray to do shell script exiftool_path & "exiftool -T -s3 -modelName -bayNumber -timeOfDay -PhotoShootName " & quoted form of thePath
		
		set {modelName, timeOfDay, bayNumber, shotFolderName} to theSplit(metadataArray, tab)
		-- Or probably:
		-- set {modelName, bayNumber, timeOfDay, shotFolderName} to theSplit(metadataArray, tab)
		
	end if
	
	
end repeat

Oops, I missed an other error.

In the original script, we may read :

set fileTypeList to {".jpg", ".psd", ".tif", ".mov", ".eip"}

The dot is NOT part of name extensions.
The correct instruction would be :

set fileTypeList to {"jpg", "psd", "tif", "mov", "eip"}

Happily, the list is not used by the real code.

I would be interested to know if the instruction triggering exifTool return the values in the order used to ask for them - what Nigel assumes - or return them in the order used in your original code :

set modelName to text item 1 of theArray
set timeOfDay to text item 2 of theArray
set bayNumber to text item 3 of theArray
set shotFolderName to text item 4 of theArray # Typo in the name of the global variable

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 25 mai 2017 12:25:26

WOW! Thanks everyone for the replies! I’m obviously very newb to AppleScript (and coding in general) so a lot of code has been pilfered from forums. But you’ve taught me a lot. So, taking all of those suggestions I’ve modified the script to the below:

on adding folder items to this_folder after receiving these_items
	set userName to short user name of (system info) as text
	set userMoveToServerFolder to "BACKUP:Move_To_Server:Selects:" as alias
	
	
	-----------------------
	-----------------------
	-- Adjust metadata and move to server/local backup
	
	set exiftool_path to "/usr/local/bin/"
	set alphaDirectoryPath to "extensis:Product_Holding:"
	set alphaDirectoryPathPOSIX to quoted form of POSIX path of alphaDirectoryPath
	set inProgressPath to "BACKUP:In_Progress:"
	set inProgressPathPOSIX to quoted form of POSIX path of inProgressPath
	set fileTypeList to {"jpg", "psd", "tif", "mov", "eip"}
	
	repeat with i from 1 to number of items in these_items
		set this_item to item i of these_items
		set thePath to quoted form of POSIX path of this_item
		tell application "Finder"
			set theFileNamePath to POSIX path of this_item
			set fileName to name of (POSIX file theFileNamePath as alias)
		end tell
		set bscName to text 1 thru 15 of fileName
		
		if fileName ends with ".jpg" then
			
			set metadataArray to do shell script exiftool_path & "exiftool -T -s3 -modelName -bayNumber -timeOfDay -PhotoShootName " & thePath
			
			set {modelName, bayNumber, timeOfDay, shootFolderName} to my theSplit(metadataArray, tab)
			
		end if
		
		display dialog "Array results:" & return & "Model Name: " & modelName & return & "Bay Number: " & bayNumber & return & "Time of Day: " & timeOfDay & return & "Shoot Name: " & shootFolderName
		
	end repeat
	
end adding folder items to


on theSplit(theString, theDelimiter)
	-- save delimiters to restore old settings
	set oldDelimiters to AppleScript's text item delimiters
	-- set delimiters to delimiter to be used
	set AppleScript's text item delimiters to theDelimiter
	-- create the array
	set theArray to every text item of theString
	-- restore the old setting
	set AppleScript's text item delimiters to oldDelimiters
	-- return the result
	
	return theArray
end theSplit

Everything is working except for the Finder call to get the fileName to see what it ends with. The script chokes when I change it to the suggested:

if thePath ends with ".jpg" then

(with quoted form of added into my exiftool call). It chokes when i just remove the:

Tell application "Finder"
end tell

I haven’t been able to get any variation to work except my original

tell application "Finder"
    set theFileNamePath to POSIX path of this_item
    set fileName to name of (POSIX file theFileNamePath as alias)
end tell

Hi.

Possibly you didn’t notice that I’d left out ‘quoted form of’ in the line which sets the thePath variable. The text in thePath can only end with “.jpg” if it doesn’t have a single quote at each end! But the quotes are necessary in the shell script.

My path suggestion was simply to save you having to get the item’s name, which you only use to derive bscName (which isn’t used) and to see if it ends with “.jpg”. But if you intend to use bscName later on, then you will need to extract the name. There are faster ways of doing it, but using the Finder’s fine if you want to keep things simple.

Marc’s already mentioned that the Finder doesn’t understand POSIX paths or POSIX files, although your code seems to work OK. However, ‘POSIX file theFileNamePath as alias’ simply reverses the effect of turning this_item into a POSIX path in the previous line. The Finder block could be more straightforwardly:

tell application "Finder"
	set fileName to name of this_item
end tell

Or:

tell application "Finder" to set fileName to name of this_item