Applescript: Subtrating substrings from strings

Discussion in 'Mac Programming' started by VideoBeagle, Oct 5, 2014.

  1. VideoBeagle macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #1
    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?
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    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.
     
  3. VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #3
    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.
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    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
     
  5. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #5
    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.
     

Share This Page