Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

oblongmedulla

macrumors newbie
Original poster
Feb 8, 2009
4
0
so I am working on a project trying to get a button when pressed to startAnimate the NSProgressIndicator bar this is an indeterminate progress bar that i would like to manually just stopanimation on

under app delegate outlets myProgressBar -- myProgressBar

in applescript-

Code:
property myProgressBar : missing value -- connected to the progress bar


  on buttonPressRepair_(sender)
       --------------------------------start animation
        set myProgressBar to (void)startAnimation:(myProgressBar)sender

        tell myProgressBar

        end tell
        display dialog "ok Done"
       --------------------------------stop animation
  end buttonPressRepair_

thanks for any help.
 
The start and stop methods need to be converted to AppleScriptObjC, which doesn't use the type definitions, so in Mavericks they would just be startAnimation:sender and stopAnimation:sender. The sender is the object sending the message - it isn't used for anything here, but is referring the current script object, which would usually be 'me':

Code:
property myProgressBar : missing value -- connected to the progress bar
property animated : false -- keeps track of progress bar animation

  on buttonPressRepair_(sender) -- toggle animation
    if animated then
      myProgressBar's stopAnimation:me -- one way
      set animated to false
    else
      tell myProgressBar to startAnimation:me -- another way
      set animated to true
    end if
  end buttonPressRepair_
 
Thanks alot Red Menace!! I was able to get it to work- here is the code I ended up with, I am sure there are other issues with my code but it does work :) Any big issues with this? :)

Code:
on buttonPressRepair_(sender) -- toggle animation
        tell myProgressBar to startAnimation:me -- another way
        set animated to true
        try
            do shell script "killall -HUP mDNSResponder"  with administrator privileges
            do shell script "killall -INFO mDNSResponder"  with administrator privileges
            do shell script "dscacheutil -flushcache"  with administrator privileges
            
            on error
            display dialog "Sorry, there was a problem!"
        end try
            do shell script "sleep 5"
            display dialog "Successfully cleared your DNS Cache!" Buttons ("ok")
            tell myProgressBar to stopAnimation:me -- another way
            set animated to false

also, perhaps you could answer something I have been curious about- is there a way with ASOC/xcode/cocoa to directly change osx system settings, I.E. mirroring, or resolution, without using the applescript activate front window tell group 1 to click radio button etc...

Like an actual osx command @OSX Mirroring = ON & Resolution set to "1280x960"
 
In my example, I was using a property to keep track of whether or not the progress indicator was animating so that it could be toggled by using the same button method - normally you don't need to keep track, since you usually know when you have turned it on or off.

The main thing you need to realize about those preferences and utilities is that they just put a fancy user interface on top of the code that actually does something. AppleScriptObjC can use most of the Cocoa classes, but there are a few things that it can't do very well (or at all), such as pointers, blocks, and C functions, so it depends on what you are wanting to do. The system preferences are usually buried in a configuration file somewhere, so you would need to know where the preference is kept in addition to the code statements needed to perform the setting, if there isn't a handy shell utility to do it.
 
aye, thanks again, for you help. Much appreciated.

So I see you are mentioning you set the properties to control the animation "automatically" using that script. I think I am seeing that now- So how would I use that in code?

would this work? or how would I go about adding the do shell commands and dialog using your method?
Code:
property myProgressBar : missing value -- connected to the progress bar
property animated : false -- keeps track of progress bar animation

  on buttonPressRepair_(sender) -- toggle animation
    if animated then
      myProgressBar's stopAnimation:me -- one way
      set animated to false
    else
      tell myProgressBar to startAnimation:me -- another way
      set animated to true
    end if
  end buttonPressRepair_


on buttonPressRepair_(sender) -- toggle animation
try
            do shell script "killall -HUP mDNSResponder"  with administrator privileges
            do shell script "killall -INFO mDNSResponder"  with administrator privileges
            do shell script "dscacheutil -flushcache"  with administrator privileges
            
            on error
            display dialog "Sorry, there was a problem!"
        end try
            do shell script "sleep 5"
            display dialog "Successfully cleared your DNS Cache!" Buttons ("ok")
end buttonPressRepair_
 
You can't have multiple handlers with the same name. I would probably do something like:

Code:
property myProgressBar : missing value -- connected to the progress bar

on buttonPressRepair:sender
	myProgressBar's startAnimation:me
	try
		do shell script "killall -HUP mDNSResponder" with administrator privileges
		do shell script "killall -INFO mDNSResponder" with administrator privileges
		do shell script "dscacheutil -flushcache" with administrator privileges
		display dialog "Successfully cleared your DNS Cache!" buttons {"ok"}
	on error
		display dialog "Sorry, there was a problem!" buttons {"ok"}
	end try
	myProgressBar's stopAnimation:me
end buttonPressRepair:

Note that you should be careful when using commands that may take a while to complete (including sleep), since the user interface will be blocked unless you do something like run shell scripts in a background task or periodically run a method to handle events.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.