Hey guys,
You’ve been very helpful in the past so I wanted to set you guys loose on this one.
I have 7 Checkboxes. One for each day of the week.
Whichever days the user has selected decides if the app will launch. I am having problems setting this up. I can bind a text field named “scheduledDays” where the user can manually type “Monday, Tuesday, Friday” or whatever
and I can script
set workDays to content of default entry "scheduledDays"
…
if workDays contains ((current date)'s weekday) then
tell application "whatever"
activate
end tell
but I cannot figure out how to use this same concept with checkboxes.
This route sounds like the easiest. How do I get it to write to the text field? Let me rephrase that…how can I get it to ADD content to the text field?
If I wrote:
if state of button "monday" of window 1 is 1 then
set contents of text field "workDays" to "Monday"
Wouldn’t this just set the ENTIRE text field contents to Monday? What if Monday and Tuesday are checked?
property allWeekdays : {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
set wkList to {}
repeat with oneWeekday in allWeekdays
if state of button oneWeekday of window 1 is 1 then
set end of wkList to contents of oneWeekday -- add checked days to the list
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, ", "}
set wkList to wkList as Unicode text -- flatten the list e.g. {"Monday", "Thursday"} to "Monday, Thursday"
set text item delimiters to TID
set contents of text field "workDays" to wkList
I’m not entirely clear as to what you’re trying to do. Are you implementing this to control the behavior of your app, or to control the launching of another app? If you’re trying to make this a preference within your own app, perhaps you haven’t thought of an important factor… that your app won’t launch on days that aren’t set as launch days, so you can only change your settings on checked days.
If you’re using this as some sort of launcher or stay-open helper app, then this behavior is valid…and quite simple to implement. I’m not sure where the other guys came up with the whole text field thing, because from your posts I see no mention of wanting to display the days selected (that should be pretty obvious from the checkboxes that are checked ;)). Using hidden objects to hold data representations or object references like this is rarely necessary. If all you want to do is test to see if a key is saved, that’s a simple task, and can be done in a variety of ways and from a variety of handlers… depending on your needs. If you understand bindings, simply bind each checkbox to your shared user defaults. For each checkbox, under the “Value” binding, in the “Model Key Path” field enter the key you want to use to save the value in the defaults file. I used keys preceded by ‘launch’, for example “launchSunday”. Then I created a simple handler that checks the current value for any given day against the user defaults and returns true or false for whether it should launch or not on that day. In my test app, I used the will finish launching handler, but you could put the two lines found in that handler anywhere and it would launch the app.
on will finish launching theObject
set dayToday to (weekday of (current date)) as string
if (shouldLaunchOnDay(dayToday)) then tell application "Chess" to activate
end will finish launching
to shouldLaunchOnDay(theDay)
set theDay to ("launch" & theDay) as string
tell user defaults
if exists default entry theDay then
return ((contents of default entry theDay) as boolean)
else
make new default entry at end of default entries with properties {name:theDay, contents:no}
return no
end if
end tell
end shouldLaunchOnDay
Using bindings for simple defaults entries means zero code to get and set data to file. Reading data from the defaults is exactly the same as if you were managing your default entries manually. Hope this is what you’re looking for.
excellent posts guys!
I found the resolution in a mixture of both posts.
Let me explain exactly what is happening because a new issue has arrived.
I have created an app that will connect to a FTP server and download any text files from a particular folder, then delete them. I have all of this on a schedule using on idle and repeat.
The user can set how often the app runs this task. I have a binding on a text field named “repeatIn” to save that data in its prefs. The problem is the program does not read this pref file until the next time it runs.
If the user has the schedule set to 60 minutes and changes it to 30 seconds, it does not take affect until the next time it is run (potentially another 60 minutes).
How do I get the app to instantly recognize the change?
Once again, thanks so much for your help. You guys are awesome.