Apple_For_The_Win

Q: How do I make a variable path in AppleScript?

I'm mass distributing an application, and for the next update I want to add customization. My application currently make a folder in the "Documents" folder. The folder is called "MCL_CONFIG". What I need to do is make a unicode text file in "MCL_CONFIG". Since it's going to be used on many different computers the path to where I'm making the new file has to change depending on the user. How can I make a variable path to a non-directory? Here's the section of my code with the problem:

 

tell application "Finder"

    if folder MCLConfig exists then

    else

        display dialog "Welcome to Mc Launcher! You will only recieve this message once, unless you delete the MCL_CONFIG folder." buttons {"Ok"} default button 1

        make new folder at New_User with properties {name:"MCL_CONFIG"}

    end if

end tell

 

It's not shown but I set the variable MCLCONFIG to (path to documents folder from user domain) & "MCL_CONFIG" as text.

 

As a side note how do I make the code look like it does in applescript, here?

Posted on Mar 13, 2013 1:53 PM

Close

Q: How do I make a variable path in AppleScript?

  • All replies
  • Helpful answers

  • by red_menace,

    red_menace red_menace Mar 13, 2013 6:40 PM in response to Apple_For_The_Win
    Level 6 (15,526 points)
    Desktops
    Mar 13, 2013 6:40 PM in response to Apple_For_The_Win

    It looks like you have answered your own question, in that Standard Additions has paths to various standard locations.  However, an application that you are "mass distributing" should not put anything in a user folder such as Documents - the proper location to use is a folder in ~/Library/Application Support.

  • by Apple_For_The_Win,

    Apple_For_The_Win Apple_For_The_Win Mar 13, 2013 9:29 PM in response to red_menace
    Level 1 (4 points)
    Mac OS X
    Mar 13, 2013 9:29 PM in response to red_menace

    How would I create a folder there?

  • by red_menace,Solvedanswer

    red_menace red_menace Mar 13, 2013 9:53 PM in response to Apple_For_The_Win
    Level 6 (15,526 points)
    Desktops
    Mar 13, 2013 9:53 PM in response to Apple_For_The_Win

    You can do something like:

     

    set supportFolder to ((path to application support from user domain) as text)

    set appName to "My Spiffy App"

    set subFolder to "MCL_CONFIG"

     

    tell application "Finder" -- add application support folders if needed

      if not (exists folder (supportFolder & appName)) then

        make new folder at folder supportFolder with properties {name:appName}

        make new folder at result with properties {name:subFolder}

      end if

    end tell


    Edit: it is recommended that the folder in Application Support be named with your application's bundle identifier, but most just use the application name.

  • by softwater,

    softwater softwater Mar 14, 2013 12:46 AM in response to Apple_For_The_Win
    Level 5 (5,392 points)
    Mac OS X
    Mar 14, 2013 12:46 AM in response to Apple_For_The_Win

    Depending on what 'customizations' you want to offer, you may be able to do away with the need for a config file.

     

    If, for example, you just need to have variable values persist from one run to the next, you can do that by defining a property in your script, rather than using variables whose values have to be read in from the config file every time.

     

    See "Variables and Properties", p53 of The AppleScript Language Guide.

  • by Apple_For_The_Win,Helpful

    Apple_For_The_Win Apple_For_The_Win Mar 14, 2013 7:50 AM in response to red_menace
    Level 1 (4 points)
    Mac OS X
    Mar 14, 2013 7:50 AM in response to red_menace

    Thank you so much! You have put an end to my two week search!