Archiv der Kategorie: Applescript

Applescript: TextEdit example

tell application "TextEdit"
	activate
	close every document saving no
	make new document at the beginning of documents
	set the name of window 1 to "Font Samples"
	set zoomed of the front window to true
	set the text of the front document to "The quick brown fox jumped over the lazy dog."
	tell the text of the front document
		set the font to "Lucinda Grande"
		set the size to 24
	end tell
end tell

AppleScript: Terminal – check mail with pearl

set ret to my email_check("yourmail@domain.xy")

on email_check(mailadress)
    -- 29. Mai 2009, 1.55 Uhr, erarbeitet von H =:o) L G I und macmissionar@gmail.com 
    try
        return (run script (do shell script "perl -nle 'if (/^[^\\,.][A-Z0-9._%+-]+[^\\,.]@[^\\.,](?:[A-Z0-9._%+-]{2,}\\.)[A-Z]{2,4}$/i) {print \"true\"} else {print \"false\"}' <<< " & (quoted form of mailadress)))
    on error fehler
        return fehler
    end try
end email_check

AppleScript: Terminal – curl

(* These examples uses the curl command, see man pages for further details. *)

--GET Example
property PathToServer : "http://www.qusa.de"
set cmd to "curl -s " & quoted form of (PathToServer & "?com=show")
set ReturnValue to do shell script cmd

--POST Example
property PathToServer2 : "http://www.qusa.de"
set cmd2 to "curl  -s '" & quoted form of (PathToServer2 & "?id=59") & "' -d 'xy=51&name2=value2&press=ok'"
set ReturnValue2 to do shell script cmd2

AppleScript: System Events – writing to plist-file

tell application "System Events"
	-- create an empty property list dictionary item
	set the parent_dictionary to make new property list item with properties {kind:record}
	-- create new property list file using the empty dictionary list item as contents
	set the plistfile_path to "~/Desktop/example.plist"
	set pl to make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
	-- add the values to plist
	tell pl
		-- create a text entry
		make new property list item at end with properties {kind:string, name:"testtext", value:"This is some text"}

		-- create a truth value entry
		make new property list item at end with properties {kind:boolean, name:"testbool", value:true}

		-- create an integer entry
		make new property list item at end with properties {kind:number, name:"testinteger", value:250}

		-- create a float / real entry
		make new property list item at end with properties {kind:number, name:"testfloat", value:123.456789}

		-- create an empty dictionary
		set r to make new property list item at end with properties {kind:record, name:"testdictionary"}

		-- fill that dictionary
		tell r
			-- some string whith non ASCII characters
			make new property list item at end with properties {kind:string, name:"danish_island", value:"XY"}

			-- some date entry
			make new property list item at end with properties {kind:date, name:"now", value:current date}

			-- create an array (or list)
			make new property list item at end with properties {kind:list, name:"testarray", value:{123, "abc", 100}}
		end tell
	end tell
end tell

(*another example to write an plist-file*)
set plistfile_path to "~/Desktop/example_2.plist"
set the_record to {testbool:true, testinteger:250, testdictionary:{danish_island:"XY", now:date "Donnerstag, 16. Juni 2011 14:38:04", testarray:{123, "abc", 100}}, testtext:"This is some text", testfloat:123.456789}
tell application "System Events"
	set the parent_dictionary to make new property list item with properties {kind:record, value:the_record}
	set pl to make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
end tell