Toggle adapter status with applescript on Lion

Note: this script requires Satimage’s applescript additions(regex in particular) available here(they are downloadable together with smile).

//this script needs the scripting additions of Smile
set servList to do shell script ("networksetup -listnetworkserviceorder")

set match to ""

set match to find text "^\\([0-9*]+\\)[^
]+" in servList with regexp and all occurrences
end try

set devList to {}
set lan to 1
repeat with dev in match
set devName to matchResult of dev
set devList to devList & {devName}
end repeat

set devBox to choose from list devList without multiple selections allowed
if devBox is false then
end if
set devAnswer to item 1 of devBox

set servName to ""
set servName to matchResult of (find text "^\\([0-9*]+\\)([^
]+)" in devAnswer using "\\1" with regexp)
end try

if (offset of "*" in devAnswer) > 0 then
do shell script ("networksetup -setnetworkserviceenabled " & servName & " on")
display alert devAnswer & " has been enabled!" as informational buttons {"Ok"} giving up after 5
do shell script ("networksetup -setnetworkserviceenabled " & servName & " off")
display alert devAnswer & " has been disabled!" as informational buttons {"Ok"} giving up after 5
end if

Dynamic method calling from applescript

Applescript comes handy sometimes when you need to do some automation job quick within the MAC OS. Though it lacks many of the features offered by the modern languages, you can sometimes simulate them doing some dirty tricks :). One of them is dynamic calling of the methods of a particular script. let’s take the case where you want for example to store some options for your script in a separate file though it might look like a fancy way of storing options. so it will look something like

on AppleOptions()
  return some options
end AppleOptions

on GrapesOptions()
  return some options
end GrapesOptions


on Options()
  --this should be a default option 
  return some sort of options
end Options

Now if the calling of these options depends on some context that changes every time you run your script, you can still call your methods without knowing beforehand what’s going to be called, like this:

set theFruit to some_value_that_came_from_current_context
set selScpt to path_to_the_options_script_file
set selScpt to load script selScpt
set scptOpt to Options() of selScpt
set _scptOpt to run script ("on run (inArgs)" & return & "set theCaller to item 1 of inArgs" & return & "tell (item 1 of inArgs) to " & theFruit & "Options()" & return & "end run") with parameters {selScpt}
--optionally check for empty list or string returned in the try block
end try
if (_scptOpt is not "" and _scptOpt is not {}) then
--display dialog "ok"
set scptOpt to _scptOpt
end if

Calling the above script with theFruit set as “Apple” (no case checking in the above script though) will call your imported script’s AppleOptions() or if not found will get the default Options method.

[rb] Removing textframe linkage with applescript

You may sometimes need to be able to remove linkage between text frames in a particular document in Indesign. this script might come to the rescue:

tell application "Adobe InDesign CS2"
	set theObjs to selection
	if theObjs is equal to {} then
		set theObjs to every text frame of document 1
	end if
	tell document 1 to set thebaseframe to make new text frame
	repeat with tos in theObjs
		if (class of tos as string) = "text frame" then
			if end text frame of tos is not tos then
				set next text frame of tos to next text frame of thebaseframe
				set previous text frame of tos to previous text frame of thebaseframe
				set autoflow thread orphaned of tos to true
			end if
		end if
	end repeat
	tell document 1 to delete thebaseframe
end tell

Extending illustrator capabilities with applescript

While Illustrator offers a range of selection options that most of the time are enough for a particular scope, you might sometimes need to deal with a lot of stuff that come from other programs. This tiny script scrap might  come to the rescue in many occasions:

tell application "Adobe Illustrator"
	set sel to selection
	set target to item 1 of sel
	set cls to class of target
	set objs to every page item of document 1
	repeat with o in objs
		if (class of o is equal to cls) then
			set selected of o to true
		end if
	end repeat
end tell

Start/stop printer queue with applescript

This little applescript script will toggle the enabled/disabled status of your printer:

--be aware of the fact that HP LaserJet 5100 Series for example has the queue name HP_LaserJet_5100_Series(spaces replaced with _)
property lp_queue_name : "printer name"
on run
set isDisabled to do shell script ("lpstat -t | grep '" & lp_queue_name & " disabled'")
if isDisabled is not equal to "" then
do shell script ("/usr/bin/enable " & lp_queue_name & "; lprm -P \"" & lp_queue_name & "\" -E")
do shell script ("/usr/bin/disable " & lp_queue_name & "; lprm -P \"" & lp_queue_name & "\" -E")
end if
end run