Extracting parts of a path consistantly.

I am not really sure how to word this but I will try my best.

I have a script that gets the path of a selected file. The path used looks like this:


I am trying to extract the names of the folders PROJECTNAME and MEDIATYPE as text. Note these folders names change depending on the Project’s Name and Media type being used.

The good news is that the MEDIA folder is always placed in the XSAN folder, the PROJECTNAME folder is always placed in the MEDIA Folder and the SOURCETYPE folder is always placed in the PROJECTNAME folder.

No matter how long the path might get i need to be able to extract those two folders name consistently.

I hope this is all clear. Any help would be great. Thank you.

Maybe this code may help :

set thePath to result
set asList to my decoupe(thePath, "/")
set mediatype to item 2 of asList
set projectname to item 3 of asList


on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe


Yvan KOENIG (VALLAURIS, France) dimanche 23 février 2014 12:20:28

Since they are always going to be items 3 and 4

You could simply use a bit of awk and in a do shell script


set projectName to (do shell script "echo " & quoted form of path_ & "|awk  -F\\/ '{print $3}'")
set sourceType to (do shell script "echo " & quoted form of path_ & "|awk  -F\\/ '{print $4}'")

I just noticed you do not have a reference to “MEDIATYPE” in your path.

I assumed you wanted PROJECTNAME_ and SOURCETYPE i.e printed fields $3 and $4

If you mean “MEDIA” i.e MEDIA and PROJECTNAME_ then the printed fields should be $2 and $3

Thank you so much Mark! This solved my issue simply, so If I wanted to use a POSIX path instead how would the script look? Thanks again.

The script I used is using a Posix path. i.e with slashes “/” do you mean a posix file path. With colons “:”

Oh, I guess I am confused on what POSIX paths are then. I thought a UNIX path was with Slashes and POSIX path was with Colons. However, I do mean with colons.