AppleScript change craracter x to character y

Discussion in 'Mac Programming' started by lynkynpark86, Mar 29, 2010.

  1. lynkynpark86 macrumors 6502

    #1
    I am making an AppleScript Studio application, and it gets a file path in the "/Users/admin/Desktop" format, but I need a "Users:admin: Desktop format. So I want it to delete the first "/" in the file path and then change all further "/" to ":". Please HELP!!!
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
  3. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    Here's a generic function to replaces strings within a string:

    Code:
    on replaceString(theText, oldString, newString)
    	set AppleScript's text item delimiters to oldString
    	set tempList to every text item of theText
    	set AppleScript's text item delimiters to newString
    	set theText to the tempList as string
    	set AppleScript's text item delimiters to ""
    	return theText
    end replaceString
    However, in AppleScript I think you could just coerce it from a POSIX path to an HFS-style string (which uses colons as path separators):

    Code:
    POSIX file ("Your/full/unix/path/here") as string
    ...which results in...

    Code:
    ":Your:full:unix:path:here"
    I guess you would then just take everything except the first character to remove the leading colon.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #4
    The HFS-style pathname shown is in exact correspondence to the Posix-style pathname. Both are relative pathnames: they refer to a subpath relative to the working directory.
    http://en.wikipedia.org/wiki/Working_directory

    Leading-colon means "relative path" and no-leading-colon means absolute in HFS-style. Leading-slash means "absolute path" and no-leading-slash means relative in Posix-style.

    So if you removed the leading colon, you'd have a completely different meaning. It would correspond to "/Your/full/unix/path/here".

    Other than that, the suggestion is correct: let the system do the pathname conversion. And if the code needed an alias reference, then "as alias" is more suitable than "as string".
     
  5. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #5
    Yes, except the OP specifically showed in his example that he wanted the leading colon removed (why, I don't know, maybe he has some other use for that form).
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #6
    The OP specifically said he had:
    Note the leading slash on the Posix-style path, underlined above. Since "leading slash" corresponds to "no leading colon", and means "absolute pathname", the OP will get a path without a leading colon if he uses the Posix file class in the recommended way.

    If the OP does need to remove anything, it will be the name of the boot drive, not simply a leading colon. Because given the Posix path he's starting with, he will get an initial drive-name, as in "MacHD:Users:...", not the "Users:..." pathname he thinks he will be getting.

    I suggest verifying this by simple testing with the Posix file class in AppleScript Editor.app:

    I realize I made an error in saying "Your:path:here" corresponded to "/Your/path/here". Absolute HFS-style pathnames always start with a drive-name and no colon. Absolute posix-style pathnames always start with a "/" and may or may not include anything resembling a drive-name. Correspondence can be a tricky business, so it's best to let the Posix path and alias classes handle it.
     

Share This Page