Loading popup button menu from preferences

How do you set a popup button to load it’s menu item from saved preferences?

I have the preferences saving ok, but the menu does not update on launch (all my other prefs do).

I think the problem is in the syntax used to refer to the popup button, what should it be? should I tell the button to set it’s item to ’ item “Save” ’ for example, or should it be ’ item 1 ’ , ’ item 2 ’ etc???

thanks for any help

set menuTitles to {"test1","test2","test3"}
delete every menu item of menu of popup button "x" of window "y"
repeat with i in menuTitles
	make new menu item at the end of menu items of menu of popup button "x" of window "y" with properties {title:i, enabled:true}
end repeat

thanks for the reply, I tried that but I don’t think it s quite what I’m after. I’ll explain:

(this is my first AS studio project, and I’m fairly new to AS too, so bear with me)

I have a popop button “axnMenu” with two default menu items: “Save only” and “Save and archive”

I have the menu item saving to the preferences on quit:


set axnMenu to title of current menu item of popup button "axnMenu" of window "main"
	if axnMenu is "Save only" then
		set axnMenuis to 1
	else
		set axnMenuis to 2
	end if

and then


tell default entry "saveType" to set contents to axnMenuis

this will save the prefernces, but they will not load at startup (my prefs for window postion, slider postions etc all load correctly)

this is what I have tried to load the pref for the popup at startup:

try
		set axnMenu to contents of default entry "saveType" of user defaults
		
		try
			if axnMenu is "1" then set the current menu item of popup button "axnMenu" of window "main" to "Save Only"
			
		end try
	on error
		
	end try

If anyone can help me on this I would be most grateful.

thanks,
arum

So, you wish set up automatically the default menu item. This may work:

set contents of popup button "x" of window "y" to 0

Where “0” is the first item, “1” the second one, and so on…

Two things. First, you saved the default as an integer but then you’re testing for a string. Second, and this is slightly counter-intuitive, to set the menu item selection of a popup button, you set its title to the contents of the menu item like so:

set axnMenu to contents of default entry "saveType" of user defaults
if axnMenu is 1 then set title of popup button "axnMenu" of window "main" to "Save Only"

Jon

ok, I’ll try that jon, thanks

Ok, I tried all your suggestions, and everything I coud think of - but I can’t get this to work.

So forgetting the preferences for a moment, my problem is how to change the popup menu item from one to the other (without the user physically changing it with the mouse).
If someone could give me a clear example code of how to do this I would be so grateful. It needs to be one of my two options (ie, any exapmple will do but there needs to be two fixed options for the popup menu - someone gave me an example before shich changed the contents of the current menu item, but then both menu items where the same!!)

I’m really stuck on this, so help me please if you can!!

thanks a lot

arum

So, just to clarify before I give you the goods :D… you want to have a popup button that has two permanent fields and then some more dynamic fields, right?

You can still delete the menu and recreate it with both permanent and dynamic entries, but you must hard code the permanent ones into the script somewhere so they’re not lost when deleting all entries to update the menu. See the new static “Permanent” menu item being created in the code below.

Also, and quite important to making your code work…rather than just setting the title when making the new menu items, also set it’s name (i.e. “name:i”) this will set it’s applescript name of the menu item also, not just it’s title, so you can use calls to it’s name to reference it. Here I’ve set it to the same thing as your title.

set menuItems to {"Item1", "Item2", "Item3"}
delete every menu item of menu of popup button "popupButton" of window "theWindow"

-- Create a Permanent Item
make new menu item at the end of menu items of menu of popup button "popupButton" of window "theWindow" with properties {name:"Permanent1", title:"Permanent1", enabled:true}  

-- Create Dynamic Items from "menuItems" list
repeat with i in menuItems
   make new menu item at the end of menu items of menu of popup button "popupButton" of window "theWindow" with properties {name:i, title:i, enabled:true}  --Make sure to set the 'name' property, too
end repeat

You could connected the following bits of code to any handler to change the current menu item . The first bit sets the current item to the menu item named in the ‘goToItem’ variable.

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

Or you can hard code the scripting to make the current menu item change to a predetermined value…

-- Makes "Permanent1" the current menu item
set current menu item of popup button "popupButton" of window "theWindow" to menu item "Permanent1" of popup button "popupButton" of window "theWindow"

Hope this helps…
j

noooooooooooo :frowning:

thanks for trying, but no, I guess I wasn’t clear.

I only want 2 permanant menu items in the popup button, nothing more. what I want to do is set the current popup item on lainch to that which it was left on when the app is quit. I have the preferences for this saving no problem, and I can read them no problem. My problem is getting the preferences, and based on what the last menu item was, setting the popup to the appropriate item. It should be simple I know, but I can’t get it to work.

1 popup button, 2 options, remember last used option on launch. :evil:

thanks for your patience…

Ok, this is good…we need some good dialog to get this solved… :smiley:

First, what is your code for getting the contents of the preference? It should be something like…

set myPreference to (contents of default entry named "myPreference" of user defaults)

Make sure that you use “…default entry named…” not just “…default entry…”. Have you set up a test text field and been able to successfully set it’s contents to the value of the preference after it has been acquired?

Then, it should :wink: be as simple as using…

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

… to set the current menu item. Make sure all of the ‘windows’ and ‘popup buttons’ shown above are there. Also, make sure that you have the window and popup button’s applescript names set properly, and that the events you have calling the handler are connected properly.

Best of luck… :!:
j

ok, we’re getting closer. I can read the prefs, that is no problem, I tried putting them in a text field - works every time.

et current menu item of popup button "popupButton" of window "theWindow" to menu item myPreference of popup button "popupButton" of window "theWindow"

I think I may have missed out the “menu item” right before “my preference”, so I’ll try that tomorrow and let you know. (it’ getting late for AS code here in GMT +1)

thanks,
arum

Finally I got it!!! :slight_smile: :smiley:

after some help from the AS terminology reference , and lots of trial and error, this is what did it in the trick:

on will finish launching theObject
	-- make preferences entry
	make new default entry at the end of default entries of user defaults with properties {name:"savetype", contents:""}
	
	-- read preferenences
	set axnMenu to contents of default entry "savetype" of user defaults
	set title of popup button "AxnMenu" of window "main" to axnMenu
	
end will finish launching

on will quit theObject
	
	--  write preferences
	set axnMenu to title of current menu item of popup button "axnMenu" of window "main"
	
	tell user defaults
		tell default entry "saveType" to set contents to axnMenu
	end tell
	
end will quit

I’m pretty sure I tried it like this right at the beginning and it didn’t work, but at that stage it was probably that the prefs weren’t working or something. anyway, at least it works now. thanks for all your help.

now if you could suggest a way to store a expiry date for a demo version of my app that would be brilliant!! the app I am making is very simple, non-file based. can I get it (or the finder) to make an invisible file that stores a date?

I can’t get my app to make a file (it doesn’t need to make files for it’s main function, and this is my first AS Studio project), but I can make the finder to make a file. However I can’t get it set the content of the file, or change the visibility to false, I can write to the comment for the file, but that seems a bit sketchy!!

thanks again for all your help!!

arum

Great, glad you found a way to get it hooked up. It’s funny how the language specification says you can do things multiple ways, but sometimes only one set of code will yield the results you want :rolleyes:

Anyways, check out my post at your thread “Authorisation / codes and demo periods” for some comments on the status of MY work on getting a trial period system developed.

Best wishes…
j

Yeah, it was very frustrating - It’s a relief to finally have sorted it out.

I saw your reply to my other post already, sounds promising, but I need to have something really soon … If I can just make an invisible file to store a date in that would be enough for the moment… do you know if there’s a way to do that? or of a resource that may point me in the right direction?

anyway, thanks again for your time, I appreciate it…

arum

This thread seems to have a lot of good info and resources listed in it. You could also try searching this forum for “invisible AND file” or something similar to get some matches for what you’re interested in doing.

Apple developer’s web page Anatomy of a Metapackage also seems to be a good place to find some basic answers.

I also found This thread to have a lot of interesting, but unrelated info in it that is relative to people developing commercial distribution apps in AS.

j