AppleScript- path without /Users/My user Name/

Discussion in 'Mac Programming' started by MacBook MH, Mar 1, 2010.

  1. MacBook MH macrumors member

    Joined:
    Mar 7, 2009
    #1
    Hi,
    I want my app to work on every Mac, and open a file which every Mac has in the same location.

    here is what I got in AppleScript:

    tell application "RandomApp" to open POSIX file "/Users/Martin/Music/iTunes/Random file"


    i want to somehow delete the /Users/Martin so it works on different usernames as Martin.

    Thanks in advance :D
     
  2. fredthefool macrumors newbie

    Joined:
    Jun 4, 2008
    #2
    The Standard Additions Library provides you some "special folders" you are able to use in your skript without addressing a special application.

    So try with:
    Code:
    set theFile to (path to home folder as text) & "Random file"
    tell application "RandomApp"
            open (POSIX path of theFile)
    end tell
    
    You even have a special folder named "music folder" so you can play some song from there randomly:

    Code:
    set musicFiles to every file of (path to music folder) as list
    tell application "iTunes"
    	set theFile to some item of musicFiles
    	open theFile
    end tell
    
     
  3. MacBook MH thread starter macrumors member

    Joined:
    Mar 7, 2009
    #3
    What should I do? Please little more detail. I don't understand. Isn't there a easy way just to remove /Users/Martin/
    ?

    Little more detail:
    i want the red part away... every mac has its iTunes Music Library.xml in the same location.... so what is the problem? It doesn't matter to me how its done... but i need it without the users/martin to run it on other users too.

    :) thanks
     
  4. Willi Gofarr macrumors newbie

    Joined:
    Nov 30, 2009
    Location:
    London and New York
    #4
    Would

    Code:
    tell application "TextEdit" to open POSIX file "~/Music/iTunes/iTunes Music Library.xml"
    work?
     
  5. MacBook MH thread starter macrumors member

    Joined:
    Mar 7, 2009
    #5
    nope wont work... error message:

    in the /users/martin variant it works...
     
  6. Willi Gofarr macrumors newbie

    Joined:
    Nov 30, 2009
    Location:
    London and New York
    #6
    Ok.

    Try this ...

    Code:
    
    set TheFileFolder to path to music folder as string
    set TheFileFolder to TheFileFolder & "iTunes:iTunes Music Library.xml"
    tell application "TextEdit" to open file TheFileFolder
    
    
     
  7. MacBook MH thread starter macrumors member

    Joined:
    Mar 7, 2009
  8. Willi Gofarr macrumors newbie

    Joined:
    Nov 30, 2009
    Location:
    London and New York
    #8
    My pleasure!

    There's probably far more elegant ways of doing it, but at least this works!
     
  9. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #9
    This didn't work because AS doesn't take POSIX style file references in open commands and you'd need a single quote around "iTunes Music Library.xml" because of the spaces.

    This should work:

    Code:
    set filePath to "~/Music/iTunes/"
    
    set fileName to "iTunes Music Library.xml"
    
    set newFilePath to filePath & quoted form of (fileName)
    
    do shell script "open " & newFilePath
    
    mt
     
  10. Willi Gofarr macrumors newbie

    Joined:
    Nov 30, 2009
    Location:
    London and New York
    #10
    Ah. Of course!

    Like the OP, I'm newish to Applescript and can quite happily admit that my shell-scripting is very, very, rusty these days.

    Cheers.
     

Share This Page