Friday, August 22, 2014

#1 2012-12-22 02:12:32 pm

PeacheyMcKeitch
Member
From: Scotland
Registered: 2012-12-21
Posts: 37
Website

Setting a checkbox at launch depending on background app.

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.

Any help would be appreciated folks. Cheers!

Last edited by PeacheyMcKeitch (2012-12-22 02:14:06 pm)


David McKeitch Sound Services
www.davidmckeitch.com

Offline

 

#2 2012-12-22 06:25:58 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3580

Re: Setting a checkbox at launch depending on background app.

It would help if you posted what code you have already.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2012-12-23 09:28:50 am

PeacheyMcKeitch
Member
From: Scotland
Registered: 2012-12-21
Posts: 37
Website

Re: Setting a checkbox at launch depending on background app.

Here is the current checkbox code.

Applescript:

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.

Applescript:

--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_

Here is a pick of the current UI if that helps.

https://dl.dropbox.com/u/324103/Logic%2 … %20UI.tiff
Cheers again, let me know if im not being clear enough.

Last edited by PeacheyMcKeitch (2012-12-23 09:31:57 am)


David McKeitch Sound Services
www.davidmckeitch.com

Offline

 

#4 2012-12-23 04:06:31 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3580

Re: Setting a checkbox at launch depending on background app.

You need to define a property to act as a reference or outlet to the checkbox, then set its state. Something like:

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2013-01-02 08:09:12 am

PeacheyMcKeitch
Member
From: Scotland
Registered: 2012-12-21
Posts: 37
Website

Re: Setting a checkbox at launch depending on background app.

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.

Applescript:

2013-01-02 14:05:47.874 Checkbox test[627:303] *** -[AppDelegate applicationWillFinishLaunching:]: 0 doesn’t understand the setState_ message. (error -1708)

Here is my current code, its just a test project so i can get my head around this.

Applescript:

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

Cheers again, hope you had a good new year.

Last edited by PeacheyMcKeitch (2013-01-02 08:10:19 am)


David McKeitch Sound Services
www.davidmckeitch.com

Offline

 

#6 2013-01-02 03:23:08 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3580

Re: Setting a checkbox at launch depending on background app.

My mistake -- that should be:

Applescript:

property runningCheckbox: missing value


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2013-01-02 04:02:02 pm

PeacheyMcKeitch
Member
From: Scotland
Registered: 2012-12-21
Posts: 37
Website

Re: Setting a checkbox at launch depending on background app.

I had tried that before with no luck im afraid....the only way I can get "property runningCheckbox:" to show up in UI builder is to add

Applescript:

on runningCheckbox_(sender)

end runningCheckbox_

..could there be something im missing there perhaps?


David McKeitch Sound Services
www.davidmckeitch.com

Offline

 

#8 2013-01-03 04:35:05 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3580

Re: Setting a checkbox at launch depending on background app.

PeacheyMcKeitch wrote:

..could there be something im missing there perhaps?

Are you editing the script in Xcode or AppleScript Editor?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#9 2013-01-03 08:04:47 am

PeacheyMcKeitch
Member
From: Scotland
Registered: 2012-12-21
Posts: 37
Website

Re: Setting a checkbox at launch depending on background app.

Xcode 4.5.2


David McKeitch Sound Services
www.davidmckeitch.com

Offline

 

#10 2013-01-03 10:33:57 am

PeacheyMcKeitch
Member
From: Scotland
Registered: 2012-12-21
Posts: 37
Website

Re: Setting a checkbox at launch depending on background app.

If i write the script like this...

Applescript:

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 tongue.


David McKeitch Sound Services
www.davidmckeitch.com

Offline

 

#11 2013-01-03 04:36:18 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3580

Re: Setting a checkbox at launch depending on background app.

I wonder if you have Restorable checked in the Attributes Inspector for your window.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#12 2013-01-04 09:10:16 am

PeacheyMcKeitch
Member
From: Scotland
Registered: 2012-12-21
Posts: 37
Website

Re: Setting a checkbox at launch depending on background app.

Didndt seem to make a difference hmm.

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

Applescript:

on runningCheckbox_(runningCheckbox)

for example.

So i can figure out in what particular way im going wrong tongue


David McKeitch Sound Services
www.davidmckeitch.com

Offline

 

#13 2013-01-04 09:22:09 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10480
Website

Re: Setting a checkbox at launch depending on background app.

I recommend to use different names for the action and the outlet

Connect this property to the outlet of the checkBox

Applescript:

property runningCheckbox: missing value

Connect this handler to the action of the checkBox

Applescript:


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_()

Applescript:

on awakeFromNib()
   tell runningCheckbox to setState_(current application's NSOffState)
end awakeFromNib


regards

Stefan

Offline

 

#14 2013-01-04 09:59:01 am

PeacheyMcKeitch
Member
From: Scotland
Registered: 2012-12-21
Posts: 37
Website

Re: Setting a checkbox at launch depending on background app.

big_smile That seems to be working now! Think i was getting my actions and outlets mixed up.

Cheers guys, will incorporate this back into the original project and stick the code up here when its done. smile


David McKeitch Sound Services
www.davidmckeitch.com

Offline

 

#15 2013-01-04 12:15:27 pm

PeacheyMcKeitch
Member
From: Scotland
Registered: 2012-12-21
Posts: 37
Website

Re: Setting a checkbox at launch depending on background app.

The code working nicely in the actual app smile.

Applescript:

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

Applescript:

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


David McKeitch Sound Services
www.davidmckeitch.com

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.037 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)