Hi guys working on my first app and this forum has been invaluable getting it up and running!
Im working on an App to script Logic Pro to save automatically at a user defined time.
I have most of it working, but I have a checkbox in the setup app, that when enabled runs the background app (which runs the auto-save script.) When disabled quits the background app.
I would like the checkbox to reflect the state of the background app. So if a user opens up the setup app and the background app is already running…it will be ticked.
Ive got the Start/Quit code and the code to detect if the app is running working fine.But im pretty new to xcode and Im not sure how to do this.
script AppDelegate
property parent : class "NSObject"
--Determins if the background script is running
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
--start script
on startScript_(sender)
if sender's intValue() is 1 then
set helperApp to current application's NSBundle's mainBundle()'s bundlePath() as string & "/Contents/Resources/Logic Pro Autosave Helper.app"
tell application helperApp to launch
-- quit script
else if sender's intValue() is 0 then
tell application "Logic Pro Autosave helper" to quit
end if
end startScript_
Then later on in the app I currently have it set to display whether the background app is running on the click of a button. Im planning on changing this to return either a o or a 1 to the checkboxes. Once I figure out how to actually send the checkboxes a variable…hope im making sense here.
--display if the background script is running or not
on isScriptRunning_(sender)
if appIsRunning("Logic Pro Autosave Helper") then
display dialog "Background App is Running" with icon 1 buttons {"Ok"}
else
display dialog "Bakground App is not running" with icon 1 buttons {"Ok"}
end if
end isScriptRunning_
You need to define a property to act as a reference or outlet to the checkbox, then set its state. Something like:
script AppDelegate
property parent : class "NSObject"
property runningCheckbox: 0
on applicationWillFinishLaunching_(aNotification) -- at launch
tell "System Events" to set theResult to (name of processes) contains appName
if theResult is true then tell runningCheckbox to setState_(1)
end applicationWillFinishLaunching_
You connect the outlet in the UI by command-clicking on the checkbox, then dragging over the blue cube representing the app delegate, and finally clicking on the property in the dialog that appears.
For some reason the property won’t show up when I drag to the app delegate, any ideas what I could be missing?
The on applicationWillFinishLaunching_ code does seem to at least be talking to the code at the beginning though based on this message from the log though.
Here is my current code, its just a test project so i can get my head around this.
script AppDelegate
property parent : class "NSObject"
property runningCheckbox: 0
on applicationWillFinishLaunching_(aNotification)
tell runningCheckbox to setState_(1)
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
script AppDelegate
property parent : class "NSObject"
property runningCheckbox: missing value
on runningCheckbox_(runningCheckbox)
tell runningCheckbox to setState_(0)
end runningCheckbox_
on applicationWillFinishLaunching_(aNotification)
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
Then link the UI checkbox to runningCheckbox the setState_(0) command works fine…but only once the button has been clicked.
Anyway to get that bit of code to run as the script starts without using applicationWillFinnishLaunching I wonder?..will keep playing around :P.
Am I right in thinking I should be seeing “property runningCheckbox: missing value” in the UI builder regardless of whether it has any associated code?
as in
on runningCheckbox_(runningCheckbox)
for example.
So i can figure out in what particular way im going wrong
I recommend to use different names for the action and the outlet
Connect this property to the outlet of the checkBox
property runningCheckbox: missing value
Connect this handler to the action of the checkBox
on pushRunningCheckbox_(theSender) -- theSender returns a reference to the triggered UI element
set currentState to theSender's state() -- get the state of the check box
end pushRunningCheckbox_
A good place to preset defaults values is the awakeFromNib() handler, it’s called before applicationWillFinishLaunching_()
on awakeFromNib()
tell runningCheckbox to setState_(current application's NSOffState)
end awakeFromNib
script AppDelegate
property parent : class "NSObject"
property runningCheckbox: missing value
property logincheckbox: missing value
property text_field : missing value
--Determins if the background script is running
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
on awakeFromNib()
--dectect if background script is running and check the box accordingly
if appIsRunning("Logic Pro Autosave Helper") then
tell runningCheckbox to setState_(1)
else
tell runningCheckbox to setState_(0)
end if
--detect if the helper app is added to startup
tell application "System Events"
if login item "Logic Pro AutoSave Helper" exists then
tell logincheckbox to setState_(1)
else
tell logincheckbox to setState_(0)
end if
end tell
end awakeFromNib