One last different AppleScript issue

Discussion in 'Mac Programming' started by ring, Aug 1, 2013.

  1. macrumors regular

    Joined:
    Nov 17, 2011
    #1
    Basically, I wrote a script to kill the network (wifi as of now, will try and figure out ethernet in a bit) when the vpn disconnects. I wanted to add a failsafe that also killed applications, but when I added this bit of code

    PHP:
    if application "iTunes" is running then do shell script "killall iTunes"
                
    if application "uTorrent" is running then do shell script "killall uTorrent"
                
    if application "Transmission" is running then do shell script "killall Transmission"
                
    if application "Safari" is running then do shell script "killall Safari"
                
    if application "Google Chrome" is running then do shell script "killall 'Google Chrome'"
                
    if application "Palringo" is running then do shell script "killall Palringo"

    to the script, I kept having trouble with running it. I'm honestly not sure how the ifs are supposed to be used in this situation.




    I want it to do the following



    PHP:
     - if myConnection is not null and 
        - if 
    vpn is not connected

        
    kill wifi 
        
    - and also do the following "if statements":
        
        if 
    application "iTunes" is running then do shell script "killall iTunes"
        
    if application "uTorrent" is running then do shell script "killall uTorrent"
        
    if application "Transmission" is running then do shell script "killall Transmission"
        
    if application "Safari" is running then do shell script "killall Safari"
        
    if application "Google Chrome" is running then do shell script "killall 'Google Chrome'"
        
    if application "Palringo" is running then do shell script "killall Palring**b**"
    but I'm not really sure how to do that / everything I have done has failed. This ****is my code.



    PHP:
        on idle
        
        
        tell application 
    "System Events"
            
    tell current location of network preferences
                set myConnection to the service 
    "BTGuard VPN"
                
    if myConnection is not null then
                    
    if current configuration of myConnection is not connected then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
                    
                    
                
                        
    if application "iTunes" is running then do shell script "killall iTunes"
                        
    if application "uTorrent" is running then do shell script "killall uTorrent"
                        
    if application "Transmission" is running then do shell script "killall Transmission"
                        
    if application "Safari" is running then do shell script "killall Safari"
                        
    if application "Google Chrome" is running then do shell script "killall 'Google Chrome'"
                        
    if application "Palringo" is running then do shell script "killall Palringo"
                
    end if
            
    end tell
            
    return 0
        end tell
        end idle

    That's what's failing. Everything I have tried has something wrong. And correction/guidance/advice/help would be GREATLY appreciated. I either get an error that is complaining about the way I phrase the if statement, or I get an error that is saying error
    Code:
    "iTunes got an error: Can’t get running." number -1728 from «class runn» 
    but when I enter the following into a blank script, without the rest, they work fine and don't give that error

    PHP:
    if application "iTunes" is running then do shell script "killall iTunes"
                
    if application "uTorrent" is running then do shell script "killall uTorrent"
                
    if application "Transmission" is running then do shell script "killall Transmission"
                
    if application "Safari" is running then do shell script "killall Safari"
                
    if application "Google Chrome" is running then do shell script "killall 'Google Chrome'"
                
    if application "Palringo" is running then do shell script "killall Palringo"
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    The simplest solution is to remove all the 'if xx is running then' clauses, making the 'do shell script "xxx"' clauses unconditional.

    The killall command does nothing if there's no program running with the name given to it. So if uTorrent isn't running, it's harmless to run "killall uTorrent".

    I suggest reading the man page to killall. You can pass it multiple names, so it can kill a whole bunch of programs with one "do shell script".
     
  3. macrumors newbie

    Joined:
    Oct 13, 2012
    #3
    Maybe the following might work?

    Code:
    on idle
    	set listAppNames to {"iTunes", "uTorrent", "Transmission", "Safari", "Google Chrome", "Palringo"}
    	
    	tell application "System Events"
    		tell current location of network preferences
    			set myConnection to the service "BTGuard VPN"
    			if myConnection is not null then
    				if current configuration of myConnection is not connected then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
    			end if
    		end tell
    		
    		set listApplicationProcessNames to name of every application process
    		
    		repeat with textAppName in listAppNames
    			if listApplicationProcessNames contains textAppName then
    				do shell script "killall" & space & quoted form of textAppName
    			end if
    		end repeat
    	end tell
    end idle
    I think what might have been throwing the error was that you had your killall if statements enclosed in the 'tell current location of network preferences' tell block, and the current location of network preferences didn't quite know how to process the 'if iTunes is running' command.
     
  4. macrumors member

    Joined:
    Oct 23, 2007
    #4
    I want that too. I hope you get it working, and share it. Thanks for the effort...:)
     

Share This Page