Mac Applescript: Subtrating substrings from strings

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
785
10
App Q&A testing by request.
I'm writing a script that will (hopefully) go through a folder and subfolders and make a list of the paths of the files it finds. That part is for another day though.


Right now, I'm working on this part. I don't need the whole path. I want to only include the last part of the file path.
FOR EXAMPLE:

/Users/Scav/Test/Content/ReadMe's/pics/11361.jpg

I only want Content/ReadMe's/pics/11361.jpg

I've written this
Code:
tell application "Finder"
	set xfile to choose file
	set xpath to POSIX path of xfile
	set AppleScript's text item delimiters to "Content/"
	set newpath to ("Content/" & (text item 2 of xpath))
end tell
Which does what I want. BUT, can this be done without the kludgy "Content/" & (text item 2 of xpath) part?
 

Red Menace

macrumors 6502
May 29, 2011
440
78
Fruita, Colorado, USA
With AppleScript's limited string handling, kludgy is pretty much what you get. You don't necessarily need to use text item delimiters though:

Code:
set xpath to POSIX path of (choose file)
set here to offset of "/Content/" in xpath
set newpath to text (here + 1) thru -1 of path -- also handles text not found
Note that you should try to avoid putting scripting addition commands inside an application tell statement, but in this case you don't need the Finder at all.
 

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
785
10
App Q&A testing by request.
With AppleScript's limited string handling, kludgy is pretty much what you get. You don't necessarily need to use text item delimiters though:

Code:
set xpath to POSIX path of (choose file)
set here to offset of "/Content/" in xpath
set newpath to text (here + 1) thru -1 of path -- also handles text not found
Note that you should try to avoid putting scripting addition commands inside an application tell statement, but in this case you don't need the Finder at all.
AH.. I see how that works. with the "offset" it looks for the first iteration of /content/ and give's it position number. I knew you could do that with an individual character, but it didn't occur to me to do that with a word! Cool! Thanks!

Could you explain more your scripting addition comment?
I'm not sure what I did there, and even if these are just scripts for me, I'm trying to pick up good practice as I go along.
 

superscape

macrumors 6502a
Feb 12, 2008
930
218
East Riding of Yorkshire, UK
I'm writing a script that will (hopefully) go through a folder and subfolders and make a list of the paths of the files it finds. That part is for another day though.
Good advice from RedMenace. When you come to doing your file listing, I'd recommend taking a look at the 'find' command as part of a "do shell script".

Code:
e.g. find /path/to/your/folder -type f
 

Red Menace

macrumors 6502
May 29, 2011
440
78
Fruita, Colorado, USA
Could you explain more your scripting addition comment?
When you use an application tell statement, commands are sent to the target application. If the application does not know what to do with the command (i.e. it is not in its scripting dictionary), an error is thrown and AppleScript will usually try to resend the command to the next higher object in the chain.

A scripting addition command is a little bit different though - it will throw a privilege violation error, and some scripting addition commands may need administrator authentication in certain cases (see Scripting Addition Security in the 10.6 Release Notes).

Either way, the standard practice is to only target an application with the commands that it knows about. In your original script snippet, there was no need for a Finder tell statement, since no Finder commands were used.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.