Help with Applescript

Discussion in 'Mac Programming' started by mjaco, Oct 31, 2011.

  1. mjaco, Oct 31, 2011
    Last edited by a moderator: Oct 31, 2011

    mjaco macrumors newbie

    Joined:
    Dec 15, 2009
    #1
    Can anyone help me with these two Applescripts (Very Simple One! lol)

    I want to be able to make a applescript which will determine what OS is running on the machine and choose which Applescript to run.

    I want machine running Leopard to run the Leopard Airport Card script and the same in Snow but with the SL airport card script. I don't know how to do so hopefully someone will be able to help?

    These are the two AS's:

    Leopard Turn OFF Airport Card
    Code:
    do shell script "/usr/sbin/networksetup -setairportpower off"
    
    SL Turn Off Airport Card
    Code:
    --list all of the network hardware ports
    set the_ports to (do shell script "/usr/sbin/networksetup -listallhardwareports")
    set old_delims to AppleScript's text item delimiters
    try
    set AppleScript's text item delimiters to return
    set the_count to the count of text items in the_ports
    on error
    set AppleScript's text item delimiters to old_delims
    end try
    
    --get the port number of the AirPort device
    repeat with x from 1 to the_count
    if text item x of the_ports contains "AirPort" then
    set device_line to (text item (x + 1) of the_ports)
    end if
    end repeat
    set AppleScript's text item delimiters to ":"
    try
    set the_port to (text item 2 of device_line)
    set AppleScript's text item delimiters to old_delims
    on error
    set AppleScript's text item delimiters to old_delims
    end try
    
    --do shell script "/usr/sbin/networksetup -setairportpower" & the_port & " off" with administrator privileges
    
    Hope this makes sense.

    Many Thanks,
    Matt
     
  2. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #2
    Code:
    tell application "Finder" -- check whether Lion, or Snow Leopard is running
       set v to version as string
    end tell
    set versnum to (characters 1 thru 4 of v as text) as number
     
  3. mjaco thread starter macrumors newbie

    Joined:
    Dec 15, 2009
    #3
    What ??

    tell application "Finder" -- check whether Lion, or Snow Leopard is running
    set v to version as string
    end tell
    set versnum to (characters 1 thru 4 of v as text) as number

    I'm completely new to this and haven't clue how to tell finder how to check what OS is running and then tell it to run which app?

    Sorry for being a pain.
     
  4. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #4
    That's AppleScript code.
    Just paste it into your AppleScript Editor, and run it

    The "Tell Finder" ... "end tell" is called a tell block.
    It basically asks the Finder to give us the information we want, its version.
    Open the Finder's Dictionary (AppleScript Editor->File Menu->Open Dictionary) to see what you can ask the Finder to do for you.

    Here's a little fancier version of the script, that'll make the output number (10.7 for Lion, 10.6 for SL etc...) easier to notice:

    Code:
    tell application "Finder" -- check whether Lion, or Snow Leopard is running
    	set v to version as string
    end tell
    set versnum to (characters 1 thru 4 of v as text) as number
    say versnum
    display dialog versnum buttons {"OK"}
    return versnum
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    LOL. That provides how English-like AppleScript is! :p
     
  6. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #6
    It's the Northern California accent; throws em off every time. :p
     

Share This Page