AppleScript Unix / HFS路径转换

set theUnixPath to "/Users/Shared/"
(POSIX file theUnixPath) as string
--> "Macintosh HD:Users:Shared:"

set theMacOSXPath to "Macintosh HD:Users:Shared:"
POSIX path of theMacOSXPath
--> "/Users/Shared/"

AppleScript 在维也纳获取未读消息的数量(RSS-Reader)

tell application "Vienna"
set unreadCount to total unread count
return "Vienna: " & unreadCount & " unread messages"
end tell

AppleScript LowerCase文件名

for i in `ls *.cfm`
do
  orig=$i
  new=`echo $i | tr [A-Z] [a-z]`
  echo "Moving $orig --> $new"
  mv $orig $new
done

AppleScript Time Machine对Volumes的无缝网络支持

defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1

AppleScript AppleScript Skype库

(* *************************************************************
	Some helpful handlers that deal with text 
*)
(* Get all the words between the delimiters, as a list. The items do not include the delimiters
	Example: allWords of "I am the very model of a script object" between {" "}
			# => {"I", "am", "the", "very", "model", "of", "a", "script", "object"}
*)
to allWords of inString between delimiters
	set tid to text item delimiters
	set text item delimiters to delimiters
	set retval to get text items in inString
	set text item delimiters to tid
	retval
end allWords

(* find all instances of "s" in "t" and replace them with "r"
	Example: switchText of "Example text" from "e" to "."
			# => ".xampl. t.xt"
*)
to switchText of t from s to r
	set d to text item delimiters
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to set t to item 1 & ({""} & rest)
	set text item delimiters to d
	t
end switchText

(* Join the items in "stringArray" with "delimiter" and return. 
	Example: join {"X", "Y", "Z"} between "."
			# => "X.Y.Z"
*)
to join of stringArray between delimiter
	set d to text item delimiters
	set text item delimiters to {} & delimiter
	tell stringArray to set retval to item 1 & ({""} & rest)
	set text item delimiters to d
	retval
end join

(* *************************************************************
	The actual script
*)

(* Get a script object to help with skype commands *)
to makeSkypeHelper(withScriptName)
	script SkypeHelper
		property scriptName : withScriptName
		
		(* Run the "cmd" and return the result, as a list of words (as delimited by spaces)
			
			Example: cmd("GET CURRENTUSERHANDLE")
					# => {"CURRENTUSERHANDLE", "Skype.User"}
			
			If there is an error, this method will detect it and throw a proper AppleScript error.
			
			Example: cmd("INVALID CMD")
					# => throws error with number 2, message "ERROR 2 Unknown command"
		*)
		to cmd(cmd)
			tell application "Skype" to set response to send command cmd script name scriptName
			set retval to allWords of response between {" "}
			if first item of retval is equal to "ERROR" then
				error response number (item 2 of retval as number)
			end if
			retval
		end cmd
		
		(* Create chat object from the results of a cmd("SEARCH xCHATS")
			Example: chats from cmd("SEARCH CHATS")
					# => {SkypeChat, SkypeChat... }
		*)
		to chats from response
			set response to items 2 thru -1 of response
			log response
			set chatsArray to {}
			repeat with chatid in response
				if chatid ends with "," then
					set chatid to (characters 1 thru -2 of chatid) as Unicode text
				end if
				copy makeChat(chatid) to the end of chatsArray
			end repeat
			chatsArray
		end chats
		
		(* Get all the recent chats as chat objects
			Example: recentChats()
					# => {SkypeChat, SkypeChat... }
		*)
		on recentChats()
			chats from cmd("SEARCH RECENTCHATS")
		end recentChats
		
		(* Find the first chat that has the specified topic, or is a 1:1 dialog with the person named.
			Example: findChat("Skype.User")
					# => SkypeChat
			
			Example: findChat("Chat Topic")
					# => SkypeChat
			
			If no appropriate chat can be found, returns missing value		
		*)
		to findChat(topicOrSkypeLogin)
			set theChats to recentChats()
			repeat with chat in theChats
				set fn to chat's topic()
				if fn is equal to topicOrSkypeLogin then
					return chat
				end if
				
				if chat's status() is equal to "DIALOG" and chat's isSubscribed() then
					set members to chat's activeMembers()
					if (allWords of members between {" "}) contains topicOrSkypeLogin then
						return chat
					end if
				end if
			end repeat
			return missing value
		end findChat
		
		(* Send a chat message to the given target. The target can be a Chat Topic or a Skype user name.
		*)
		to sendMessage(target, message)
			set chat to findChat(target)
			if chat is equal to missing value then
				set chat to createChat(target)
			end if
			tell chat to sendMessage(message)
		end sendMessage
		
		(* Create a new chat with the given target
			Example: createChat("skype.user")
					# => SkypeChat
		*)
		to createChat(target)
			set response to cmd("CHAT CREATE " & target)
			set chatid to item 2 of response
			makeChat(chatid)
		end createChat
		
		(* Make a chat object for the already-existing Skype chat
		
			Example: makeChat("#initiating.user/$called.user;123456789abcdef")
					# => SkypeChat
		*)
		to makeChat(withChatID)
			script SkypeChat
				property parent : SkypeHelper
				property chatid : withChatID
				
				(* Get the value of the named property
					Example: skypeProperty("TOPIC")
							# => "Product Roadmap"
				*)
				on skypeProperty(propertyName)
					set response to cmd("GET CHAT " & chatid & " " & propertyName)
					set retval to join of (items 4 thru -1 of response) between {" "}
					retval
				end skypeProperty
				
				(* Get the topic of this chat
					Example: topic()
							# => "Product Roadmap"
				*)
				on topic()
					skypeProperty("TOPIC")
				end topic
				
				(* Get the status of this chat
					Example: status()
							# => "Dialog"
				*)
				on status()
					skypeProperty("STATUS")
				end status
				
				(* Find out if the current user is currently subscribed to this chat.
					When subscribed, the user can send messages to the chat and will get messages sent by others.
					
					Example: isSubscribed()
							# => true
				*)
				on isSubscribed()
					skypeProperty("MYSTATUS") is equal to "SUBSCRIBED"
				end isSubscribed
				
				(* Get a space-delimited list of all the active members of this chat
					Example: activeMembers()
							# => "jim.kirk bones.mccoy"
				*)
				on activeMembers()
					skypeProperty("ACTIVEMEMBERS")
				end activeMembers
				
				(* Send a message to this chat
					Example: sendMessage("Hello, world!")
						# => "MESSAGE 1234 STATUS SENDING"
				*)
				on sendMessage(message)
					cmd("CHATMESSAGE " & chatid & " " & message)
				end sendMessage
			end script
		end makeChat
	end script
	return SkypeHelper
end makeSkypeHelper

tell makeSkypeHelper("X")
	sendMessage("UserName", "Hello, user!")
	sendMessage("ChatName", "Hello, chatroom!")
end tell

AppleScript Applescript Export粘贴到txt文件

set snf to "echo" & " "
set theName to ""
set exportallstickies to "> "
set i to 0
set n to {}
set L to {}
set destFldr to ""

set mydestFldr to ""

if destFldr = "" then
  set destFldr to (choose folder with prompt "Choose a destination folder:") as text
  set mydestFldr to POSIX path of destFldr
end if

tell application "Stickies"
  activate
  tell application "System Events"
    tell application process "Stickies"
      set L to name of every window
      try
        repeat with awindow in L
          set m to value of text area 1 of scroll area 1 of window awindow
          set end of n to m
        end repeat
      end try
      try
        repeat with acontent in n
          set i to i + 1
          set theName to "stickies" & "_" & i as string
          set x to item i of n
          do shell script snf & "\"" & x & "\"" & " " & exportallstickies ¬
            & "\"" & mydestFldr & "\"" & theName & ".txt"
        end repeat
      end try
    end tell
  end tell
  display dialog "done"
end tell

AppleScript 通过终端关闭Mac

osascript -e 'tell application "System Events"' -e 'shut down' -e 'end tell'

AppleScript 点击菜单功能

on menu_click(mList)
	local appName, topMenu, r
	
	-- Validate our input
	if mList's length < 3 then error "Menu list is not long enough"
	
	-- Set these variables for clarity and brevity later on
	set {appName, topMenu} to (items 1 through 2 of mList)
	set r to (items 3 through (mList's length) of mList)
	
	-- This overly-long line calls the menu_recurse function with
	-- two arguments: r, and a reference to the top-level menu
	tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
		(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click


on menu_click_recurse(mList, parentObject)
	local f, r
	
	-- `f` = first item, `r` = rest of items
	set f to item 1 of mList
	if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
	
	-- either actually click the menu item, or recurse again
	tell application "System Events"
		if mList's length is 1 then
			click parentObject's menu item f
		else
			my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
		end if
	end tell
end menu_click_recurse


on run {input, parameters}
	
	tell application "Terminal" to activate
	menu_click({"Terminal", "Shell", "New Window", "Pro"})
	
end run

AppleScript AppleScript变量练习

tell application "Address Book"
	selection
	tell item 1 of result
		set theFirstName to first name
		set theEmail to value of email 1
	end tell
end tell

tell application "Mail"
	make new outgoing message with properties {visible:true, content:"你好 " & theFirstName & "."}
	tell result
		make new to recipient at end of to recipients with properties {address:theEmail}
	end tell
end tell

AppleScript Itunes吉他标签发现者

tell application "iTunes"
	if player state is playing then
		set artist_name to artist of current track
		set track_name to name of current track
	else
		set artist_name to the artist of selection of browser window 1
		set track_name to the name of selection of browser window 1
	end if
end tell

set the artist_name to replace_chars(artist_name, " ", "+")
set the track_name to replace_chars(track_name, " ", "+")

tell application "Safari"
	activate
	set the URL of the front document to ¬
		"http://www.ultimate-guitar.com/search.php?bn=" & artist_name & "&sn=" & track_name & "&type%5B%5D=1&type%5B%5D=3"
end tell


on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars