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

 

AppleScript: convert integer to string two complement

on int_2_string_twos_complement(i, base, bit_length)
	-- check for range
	set i to i as integer
	if i >= 2 ^ (bit_length - 1) then
		error "Can not convert integer to string integer too high for given bit length" number 1700
	end if
	if i < -(2 ^ (bit_length - 1)) then
		error "Can not convert integer to string integer too low for given bit length" number 1700
	end if
	if i < 0 then
		set i to i + 2 ^ bit_length
	end if
	set res to int_2_string(i, base)
	-- filling with zeroes
	-- calculate number of digitsset 
	set dc to (length of int_2_string(2 ^ bit_length - 1, base))
	repeat while length of res < dc
		set res to "0" & res
	end repeat
	return res
end int_2_string_twos_complement

 

AppleScript: convert integer to string

on int_2_string(the_int, base)
    set digit_string to "0123456789ABCDEF"
    set the_int to the_int div 1 -- cut off decimals if a float
    set e to 0

    set r to ""
    repeat while the_int > 0
        set d to (the_int mod base)
        set d to item (d + 1) of digit_string
        set r to d & r
        set e to e + 1
        set the_int to the_int div base
    end repeat
    if r = "" then
        set r to "0"
    end if
    return r
end int_2_string