File Path to string

Im having some difficulty trying to do the following. I would like to build a string (keywords) using the substructure of a given folder. This is most likely going to be on my desktop. My Idea is to have all the files sorted into subfolders and so on. The string needs to contain all the folder names all the way down to the end of the file path inc the file name separated by (comma+space). Im not sure how to go about this if folder names have more than one word. Here is an example of a file path and the string I would like to get from this:
thepath
“Marks-G5:Users:marklarsen:Desktop:Test Folder:China:Steelite International:Monaco - White:B8009MN Regency Plate 30cm.eps”
thestring
“Test Folder, China, Steelite International, Monaco - White, B8009MN Regency Plate 30cm.eps”
This is what I was trying but not sure what exactly I need to add to get it to function as I wanted.

set MyFile to choose file without invisibles
tell application "Finder"
	set thepath to file (MyFile) as string
end tell
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set X to words 5 thru (end of words) of thepath as string
set MyKeyWords to X as Unicode text
set AppleScript's text item delimiters to ASTID
--
tell application "Adobe Photoshop CS"
	set docRef to the current document
	tell docRef
		set infoRef to get info of docRef
		set keywords of infoRef to MyKeyWords as Unicode text
	end tell
end tell

Forget the Finder and Unitcode Text:

set OldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
tell application "Adobe Photoshop CS2"
	set docRef to the current document
	tell docRef
		set MyKeyWords to file path as string
		set MyKeyWords to every text item of MyKeyWords as list
		set keywords of info to items 1 through ((count of MyKeyWords) - 1) of MyKeyWords --as Unicode text
	end tell
end tell
set AppleScript's text item delimiters to OldDelim

This will add the keywords for the folder names as a list separated by semicolons as if you were entering it in PhotoShop. We get the file Path from the open document and coerce it to a list and add all the items except the last which is the file name.

Jerome, many thanks I now see my error. your code is almost identical to one of my previous attemps when I made list. I had tried “items” before trying “words” when it should have been “text items” a beginners mistake but a lesson learned. Finder was going to provide all the files in a batch process anyhow but your method looks better for this and it appears that a semicolon will do fine to separate the keywords although it say’s to use comma in the UI not as fussy as I thought. Your code does exactly what I wanted with only a basic change. many thanks again

set OldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
tell application "Adobe Photoshop CS"
	set docRef to the current document
	tell docRef
		set MyKeyWords to file path as string
		set MyKeyWords to text items of MyKeyWords as list
		set keywords of info to items 5 through (count of MyKeyWords) of MyKeyWords --as Unicode text
	end tell
end tell
set AppleScript's text item delimiters to OldDelim

Another way to skin a cat

set MyFile to choose file without invisibles
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set tempWords to (text items of (MyFile as string))
set AppleScript's text item delimiters to {", "}
set MyKewords to (text items 4 through (count of text items of tempWords) of (tempWords as string) as string)
set ASTID to AppleScript's text item delimiters

Or possibly even:


set {d, text item delimiters} to {text item delimiters, ":"}
tell application "Adobe Photoshop CS2" to tell current document to set info's keywords to (file path as string)'s text items 5 through -1
set text item delimiters to d


:wink: