John McLane John McLane - 1 year ago 83
AppleScript Question

XCode + Applescript App | Add Preferences to the App

I created an App that start stop system daemons. I also created a preferences

inside the project and linked to the
Preferences (⌘,)
menu item. Inside this preferences window I have a few
Text Field
boxes like Apache Path, MySQL Path and so on.

I want to "link" these boxes to the Info.plist of the app (or force the app to create it's own plist inside Application Support). Whenever these are edited and the user click OK the plist should also be written.

I also need to read these from the plist for the
so it gets populated when the app starts

I've tried many different approaches, but none worked. The plist is never written, or read.

Answer Source

Solved! Here is how:

The IBActions:

property configOK : missing value
property apacheLocation : ""
property pathApache : missing value

Initialisation, read the value from the app domain plist (if exists, if not try to detect)

on applicationWillFinishLaunching_(aNotification)

tell standardUserDefaults() of current application's NSUserDefaults

    set apacheLocation to objectForKey_("apacheLocation") as text
    if apacheLocation is "" then
        pathApache's setStringValue:(do shell script "source $HOME/.bash_profile; which apachectl")
        pathApache's setStringValue: objectForKey_("apacheLocation")
    end if end tell

Save prefs variables upon "Prefs OK Button" clicked

on configOK_(sender)
    set apacheLocation to pathApache's StringValue() as text

App is ending, write the values to the plist

on applicationShouldTerminate_(sender)
    tell standardUserDefaults() of current application's NSUserDefaults
        setObject_forKey_(apacheLocation, "apacheLocation")
    end tell

Here is the App :D btw: my little nasty app