Preserving values across executions of AppleScript application

I have a property schoolserv that is set by the choose menu item handler. Since it is a property the value should stay across executions. I’m trying to get the popup menu (drop down style) to return to the previous state depending on the value of the schoolserv property. I’ve tried the below code to have it set the current menu item with a few changes but none of them work. How should I write this or what handler should I attach it to so that I can make the application appear to have the values still entered after opening it?

Code used to set value of schoolserv:
on choose menu item theObject
if name of theObject is “nochoice” then
set schoolserv to “none”
tellthem(“You must choose where to backup your work.”)
else if name of theObject is “deeprun” then
set schoolserv to “deeprun-students”
tellthem(“”)
else if name of theObject is “freeman” then
set schoolserv to “freeman-students”
tellthem(“”)
else if name of theObject is “godwin” then
set schoolserv to “godwin-students”
tellthem(“”)
else if name of theObject is “henrico” then
set schoolserv to “henrico-students”
tellthem(“”)
else if name of theObject is “hshs” then
set schoolserv to “hshs-students”
tellthem(“”)
else if name of theObject is “tucker” then
set schoolserv to “tucker-students”
tellthem(“”)
else if name of theObject is “randolph” then
set schoolserv to “randolph-students”
tellthem(“”)
else if name of theObject is “varina” then
set schoolserv to “varina-students”
tellthem(“”)
end if
end choose menu item

Trying to set popup button to current value of schoolserv on start:
on will finish launching theObject
if schoolserv is “none” then
set current menu item of popup button “chosenschool” of window “main” to “hshs”
else if schoolserv is “deeprun-students” then
set current menu item of popup button “chosenschool” of window “main” to “deeprun”
else if schoolserv is “freeman-students” then
set current menu item of popup button 1 of window 1 to menu item 2 of menu “popmenu” of popup button 1 of window 1
else if schoolserv is “godwin-students” then
set current menu item of popup button “chosenschool” of window “main” to “godwin”
else if schoolserv is “henrico-students” then
set current menu item of popup button “chosenschool” of window “main” to “henrico”
else if schoolserv is “hshs-students” then
set current menu item of popup button “chosenschool” of window “main” to “hshs”
else if schoolserv is “tucker-students” then
set current menu item of popup button “chosenschool” of window “main” to “tucker”
else if schoolserv is “randolph-students” then
set current menu item of popup button “chosenschool” of window “main” to “randolph”
else if schoolserv is “varina-students” then
set current menu item of popup button “chosenschool” of window “main” to “varina”
end if
end will finish launching

I think you may want to use the “awake from nib” handler for either the popup button or maybe the parent window.

Just throwing out a guess.

Do you mean that you want to set the value of the variable “schoolserv” to something new, quit your app, and then open it again with the new value saved in “schoolserv”? If so, I don’t think you can do that. You can use properties like that in a simple applescript, but not in an AS studio application. There’s more going on than just a script, since you’re actually creating a compiled cocoa app in the end. Hopefully someone will prove me wrong, but I have yet to make that work. To set a value that is persistent through a relaunch of the app, you probably want to save the data to preferences (user defaults) or some other method of storing data externally. You’ll have to be more specific about what you’re trying to store and how it’s formatted to give you much more help on storing the data and retrieving it. If you want to go the user default route, there’s a lot of info in This Thread that’ll get you started.

If you can attach the popup button itself to the on choose menu item handler, not the individual items, it might make it a bit easier to organize the handler. You could replace your choose menu item handler with something like this…

on choose menu item theObject
  set theTitle to title of theObject
  set theName to name of theObject

  if theName is "PopupButton" then -- change "PopupButton" to the name of your button!
    if theTitle is "nochoice" then
      set schoolserv to "none"
      tellthem("You must choose where to backup your work.")
    else
      set schoolserv to (theTitle & "-students")
      tellthem("")
    end if
  end if
end choose menu item

You can set the current menu item with…

set current menu item of popup button "popupButton" of window "theWindow" to menu item goToItem of popup button "popupButton" of window "theWindow"

…where “goToItem” is a variable storing the name of the item you want to set as the current menu item.

You could put your ‘initialization’ code, the code you want to use to set the current item, in a couple of places. You could put it in the awake from nib or launched, or in a window’s awake from nib or will open or opened, etc. It depends on how you’re getting the data that determines the current item and when you want it updated.

Hope this helps…post more details or obstacles here and we’ll help…
j

So the current code will make and set the plist and the like but fails to load the values back into the popup button. What should I connect the popup button to and how should I direct the awake from nib handler (ie. if it calls a certain object or leave it open so that it executes the whole block at once).

I’ve ended up with this so far:


on choose menu item theObject
	set theTitle to title of theObject
	set theName to name of theObject
	
	if theName is "chosenschool" then
		if theTitle is "-- Choose Your School --" then
			set schoolserv to "none"
			tellthem("You must choose where to backup your work.")
		else if theTitle is "Deep Run HS" then
			set schoolserv to "deeprun-students"
			tellthem("You chose Deep Run.")
		else if theTitle is "Freeman HS" then
			set schoolserv to "freeman-students"
			tellthem("You chose Freeman.")
		else if theTitle is "Godwin HS" then
			set schoolserv to "godwin-students"
			tellthem("You chose Godwin.")
		else if theTitle is "Henrico HS" then
			set schoolserv to "henrico-students"
			tellthem("You chose Henrico.")
		else if theTitle is "Highland Springs HS" then
			set schoolserv to "hshs-students"
			tellthem("You chose Highland Springs.")
		else if theTitle is "Tucker HS" then
			set schoolserv to "tucker-students"
			tellthem("You chose Tucker.")
		else if theTitle is "VA Randolph HS" then
			set schoolserv to "randolph-students"
			tellthem("You chose VA Randolph.")
		else if theTitle is "Varina HS" then
			set schoolserv to "varina-students"
			tellthem("You chose Varina.")
		end if
	end if
end choose menu item

on registerSettings()
	tell user defaults
		make new default entry at end of default entries with properties {name:"schoolserv", contents:schoolserv}
		make new default entry at end of default entries with properties {name:"theirname", contents:theirname}
		make new default entry at end of default entries with properties {name:"theirnum", contents:theirnum}
		register
	end tell
end registerSettings

on loadSettings()
	tell user defaults
		set schoolserv to contents of default entry "schoolserv" as string
		set theirname to contents of default entry "theirname" as string
		set theirnum to contents of default entry "theirnum" as string
	end tell
end loadSettings

on saveSettings()
	tell user defaults
		set contents of default entry "theirname" to theirname
		set contents of default entry "theirnum" to theirnum
		set contents of default entry "schoolserv" to schoolserv
	end tell
end saveSettings

on will finish launching theObject
	registerSettings()
	loadSettings()
end will finish launching

on will quit theObject
	saveSettings()
end will quit

on awake from nib theObject
	set theName to name of theObject
	if theName is "chosenschool" then
		if schoolserv is "none" then
			set current menu item of popup button "chosenschool" of window "main" to menu item "-- Choose Your School --" of popup button "chosenschool" of window "main"
		else if schoolserv is "deeprun-students" then
			set current menu item of popup button "chosenschool" of window "main" to menu item "Deep Run HS" of popup button "chosenschool" of window "main"
		else if schoolserv is "freeman-students" then
			set current menu item of popup button "chosenschool" of window "main" to menu 
          -----repeat for rest of schools------
		end if
	else if theObject is "name" then
		tell window "main" to set contents of text field "name" to theirname
	else if theObject is "pass" then
		tell window "main" to set contents of text field "pass" to theirnum
	end if
end awake from nib

You want to attach the awake from nib event of the WINDOW (“main”?) containing the popup button to the script, not the buttons or text fields. That way you can do all your evaluations and setup in the window at once. When you’re setting the “schoolserv” variable in the on choose menu handler, make sure you have named all of your menu items’ applescript names exactly as they’re written. You should probably check for the user defaults before trying to save them, as I don’t think setting their contents without them already existing works (see ‘will quit’ handler).

I think you’re looking for something like…

on choose menu item theObject
   set theTitle to title of theObject
   set theName to name of theObject
   
   if theName is "chosenschool" then
      if theTitle is "-- Choose Your School --" then
         set schoolserv to "none"
         tellthem("You must choose where to backup your work.")
      else if theTitle is "Deep Run HS" then
         set schoolserv to "deeprun-students"
         tellthem("You chose Deep Run.")
      else if theTitle is "Freeman HS" then
         set schoolserv to "freeman-students"
         tellthem("You chose Freeman.")
      else if theTitle is "Godwin HS" then
         set schoolserv to "godwin-students"
         tellthem("You chose Godwin.")
      else if theTitle is "Henrico HS" then
         set schoolserv to "henrico-students"
         tellthem("You chose Henrico.")
      else if theTitle is "Highland Springs HS" then
         set schoolserv to "hshs-students"
         tellthem("You chose Highland Springs.")
      else if theTitle is "Tucker HS" then
         set schoolserv to "tucker-students"
         tellthem("You chose Tucker.")
      else if theTitle is "VA Randolph HS" then
         set schoolserv to "randolph-students"
         tellthem("You chose VA Randolph.")
      else if theTitle is "Varina HS" then
         set schoolserv to "varina-students"
         tellthem("You chose Varina.")
      end if
   end if
end choose menu item

on will quit theObject
   tell user defaults
      if default entry "schoolserv" exists then
         set contents of default entry "schoolserv" to schoolserv
      else
         new default entry at end of default entries with properties {name:"schoolserv", contents:schoolserv}
      end if
      if default entry "theirname" exists then
         set contents of default entry "theirname" to theirname
      else
         new default entry at end of default entries with properties {name:"theirname", contents:theirname}
      end if
      if default entry "theirnum" exists then
         set contents of default entry "theirnum" to theirnum
      else
         new default entry at end of default entries with properties {name:"theirnum", contents:theirnum}
      end if
   end tell
end will quit

on awake from nib theObject
   tell user defaults
      set schoolserv to contents of default entry "schoolserv" as string
      set theirname to contents of default entry "theirname" as string
      set theirnum to contents of default entry "theirnum" as string
    end tell

    -- You might put this in a tell block in case the variables come back empty --
    set current menu item of popup button "chosenschool" of window "main" to menu item schoolserv of popup button "chosenschool" of window "main"
    set contents of text field "name" of window "main" to theirname
    set contents of text field "pass" of window "main" to theirnum 
    -- end tell
end awake from nib

Unless you were planning to use the code in the loadSettings(), saveSettings(), and registerSettings() subroutines again I think that they are unnecessary and extra work. The variables they use and produce do not automatically become global, you have to return them out of the handler into other handlers to use them. If you don’t want to use the code many times from different events, you’re better off coding them into the places they are needed. Deleting these also eliminated the need for your ‘on will finish launching’ handler. You had multiple actions going on in different handlers that were not talking to each other (you didn’t ‘return’ anything from them so other handlers could use the results), which is why your original code wasn’t doing anything to set the objects’ contents.

j

Thanks a ton it works great now. The only thing I forgot to mention is that schoolserv has the server address and the title and name of the menu item has the name of the school. So I included that and voila. I just had to clean up useless connections and spiff up the GUI. Works great, since the window appears then updates I just set the default text for a status message to Loading settings and after the settings change the message is cleared. Works great and looks better. Thanks for the help!