I know this question has been asked many many times already, and I’ve done my share of searching for my answers, but I seem to have just come up with more questions.
First- In IB, you can have a “Preferences” block in your menu bar. When the user selects that, how do I tell my script what to do with it? (IE- what handler is called when the user selects the preferences item?)
My guess is that it’s defined in IB? (Here’s my shot, but I haven’t had time to test it)
My “Preferences” item is a Menu item, named “Prefs” to applescript, and bound to the script correctly.
on choose menu item theObject
if name of theObject is "Prefs" then
--blah blah, open the window and whatnot
end if
end on choose menu item
something like that?
The second, which is more where I get confused, is the reading/writing/saving of actual preference values. I saw User Defaults mentioned in several posts when I searched, but all of the links were outdated, and I’m not entirely sure where to look here on my computer.
So I’ve got my prefs window open, where I define where several folders to be used are via a “choose folder” command in my script. These don’t change. So then, how do I make sure the app remembers those for the next time it’s run? And when it is run again, how do I make it load those paths again? I guess the whole preferences thing has me thrown for a loop, and I can’t seem to find any references to try and clear stuff up.
I have a little article on Bindings/User Defaults that has been moved around. I’ll drop a link here when it’s available. In the mean time, I can point you to some documentation: user-defaults
Thanks That has certainly helped. But I still have one problem.
At the moment, I’m just trying to see that my writing the prefs is working by having it load one of the pref values (In this case, the word “test”) into a text box. For whatever reason, this does not occur, nor can I find the plist file that this infor should be saved to in ~/library/preferences.
Is there any reason this isn’t showing up? I tell it to do all this stuff in the “on will launch theObject” block.
I hate to double post, but something weird is going on here…
--properties
property MemLoc : ""
property LangSelect : ""
property HideCorrupt : true
--Event Handlers
on will finish launching theObject
RegPrefs()
ReadPrefs()
end will finish launching
--on launched handler here, but it doesn't seem to be working.
on will quit theObject
WritePrefs()
end will quit
on clicked theObject
if name of theObject is "PSPbrowse" then
set disklist to list disks
set MemLoc to (choose from list disklist with prompt "Choose your PSP Memory Stick") as string
set contents of text field "psploc" of window "prefwind" to MemLoc as string
WritePrefs()
end if
end clicked
--Handlers
on RegPrefs()
tell user defaults
make new default entry at end of default entries of user defaults with properties {name:"PSPlocation", contents:MemLoc}
make new default entry at end of default entries of user defaults with properties {name:"LanguageSelect", contents:LangSelect}
make new default entry at end of default entries of user defaults with properties {name:"HideCorrupt", contents:HideCorrupt}
register
end tell
end RegPrefs
on ReadPrefs()
tell user defaults
set MemLoc to contents of default entry "PSPlocation" as string
set LangSelect to contents of default entry "LanguageSelect"
set HideCorrupt to contents of default entry "HideCorrupt" as boolean
end tell
end ReadPrefs
on WritePrefs()
tell user defaults
set contents of default entry "PSPlocation" to MemLoc
set contents of default entry "LanguageSelect" to LangSelect
set contents of default entry "HideCorrupt" to HideCorrupt
end tell
end WritePrefs
Now then, there are 2 problems, both of which are related. 1- Upon choosing my disk, I get an NSEvaluationReciever Error 4 (1), which I believe means that I’m trying to mess with something that doesn’t exist. I know this is the prefs file, because the “set contents of text field” line runs without a problem, because the text field sets itself correctly, but the others which call on the same pref, do not. This leads to 2- this error means my regprefs step isn’t working. Sure enough, upon looking, there is no preference plist file being created, dispite that I thought that the regprefs step should create it. The one time this did work, it only created a “PSPLocation” entry, not all 3 as requested. Is there something I’m doing wrong here, or is it just an example of flat out weird?
Also, dispite my binding the text windows to the pref “PSPlocation”, they do not automatically update, I had to throw in a writeprefs() to get it to work, while this was still working. I suspected something weird was going on, so I deleted the existing prefs file and tried agan, which is how I got to where I am now.
on clicked theObject
--==--==--==--==--==PREFERENCES==--==--==--==--==--==--
if name of theObject is "PSPbrowse" then
set disklist to list disks
set MemLoc to (choose from list disklist with prompt "Choose your PSP Memory Stick") as string
set contents of text field "psploc" of window "prefwind" to MemLoc as string
log MemLoc
WritePrefs()
end if
end clicked
I used MemLoc, since I’m not to familliar with using log commands…
Script runs with the same result, but I can’t find the log file anywhere >.<
In that case, my variables are showing up, but whenever I try to log the read/write/register commands, nothing comes up.
I.E-
on clicked theObject
--==--==--==--==--==PREFERENCES==--==--==--==--==--==--
if name of theObject is "PSPbrowse" then
set disklist to list disks
set MemLoc to (choose from list disklist with prompt "Choose your PSP Memory Stick") as string
set contents of text field "psploc" of window "prefwind" to MemLoc as string
log WritePrefs() --at this line, I still get the error, but nothing shows up in the box. I had to comment this line, and add 'log MemLoc' to log MemLoc, or any other variable, successfully.
end if
end clicked
Also, nothing will log, even variables, if I attempt to log anything in the ‘will finish launching’ handler, or the "will quit’ handler. Could I perhaps have that set up wrong?
That won’t log anything because WritePrefs() doesn’t return a value. Try using something like this:
on WritePrefs()
log "Started writing prefs"
tell user defaults
set contents of default entry "PSPlocation" to MemLoc
log "Wrote location"
set contents of default entry "LanguageSelect" to LangSelect
log "Wrote langauge"
set contents of default entry "HideCorrupt" to HideCorrupt
log "Wrote 'hide corrupt'"
end tell
log "Finished writing prefs"
end WritePrefs
Well, I got some weird results… first, the sections of my code where this is important… The first section is the areas where the code is called, the second is the code itself.
on will finish launching theObject
RegPrefs()
ReadPrefs()
end will finish launching
on will quit theObject
WritePrefs()
end will quit
on clicked theObject
--==--==--==--==--==PREFERENCES==--==--==--==--==--==--
if name of theObject is "PSPbrowse" then
set disklist to list disks
set MemLoc to (choose from list disklist with prompt "Choose your PSP Memory Stick") as string
set contents of text field "psploc" of window "prefwind" to MemLoc as string
WritePrefs()
end if
end clicked
--now the handlers
on RegPrefs()
log "Start Register"
tell user defaults
make new default entry at end of default entries of user defaults with properties {name:"PSPlocation", contents:MemLoc}
log "Registered location"
make new default entry at end of default entries of user defaults with properties {name:"LanguageSelect", contents:LangSelect}
log "Registered language"
make new default entry at end of default entries of user defaults with properties {name:"HideCorrupt", contents:HideCorrupt}
log "Registered Corrupt"
register
end tell
log "Finished Register"
end RegPrefs
on ReadPrefs()
log "Began Reading"
tell user defaults
set MemLoc to contents of default entry "PSPlocation" as string
log "Read Location"
set LangSelect to contents of default entry "LanguageSelect"
log "Read Language"
set HideCorrupt to contents of default entry "HideCorrupt" as boolean
log "Read Corrupt"
end tell
log "Finished Reading"
end ReadPrefs
on WritePrefs()
log "Began writing"
tell user defaults
set contents of default entry "PSPlocation" to MemLoc
log "Wrote location"
set contents of default entry "LanguageSelect" to LangSelect
log "Wrote Language"
set contents of default entry "HideCorrupt" to HideCorrupt
log "Wrote corrupt"
end tell
log "Finished Writing"
end WritePrefs
Ok, so we have 3 seperate instances where the log should show up, on will finish launching, on clicked, and on quit.
On will finished lauching shows no results, meaning none of the code there is executing for some reason, which I can’t seem to track down. On clicked reports up to “began writing” where it errors, obviously because it has nothing to write to. On quit shows nothing, either, most likely because it has nothing to write to, and since the app is quitting, it doesn’t error.
Weird… I can’t figure out why “On will finish launching theObject” and “On launched theObject” are apparently not doing anything that they are told to do…
I checked out your project briefly. Nothing is connected to these handlers; Check out “File’s Owner” in Interface Builder.
You’ll also want to change your RegPrefs routine:
tell user defaults
-- Error
make new default entry at end of default entries of user defaults with properties {}
-- Use this
make new default entry at end of default entries with properties {}