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

 

AppleScript: convert string to integer

on string_2_int(num_string, base)
    set r to 0
    set digit_string to "0123456789ABCDEF"
    set c_list to every character of num_string
    set c_list_length to count c_list
    repeat with x from c_list_length to 1 by -1
        set curr to item (c_list_length - x + 1) of c_list
        set r to r + ((offset of curr in digit_string) - 1) * (base ^ (x - 1))
    end repeat
    return r / 1
end string_2_int

 

AppleScript: num 2 hex konvertieren

on numToHex(n, res)
    -- Round the input value to the nearest whole-number. (Just in case it's fractional.)
    set n to n div 0.5 - n div 1

    script o
        property hexDigits : missing value
        property hexOut : {}
    end script

    if (n > -1) then
        -- The number's positive.
        set o's hexDigits to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}

        -- Construct a list of the hexadecimal digits, working backwards from the lowest-order one.
        set widthNow to 0
        repeat
            set beginning of o's hexOut to item (n mod 16 + 1) of o's hexDigits
            set n to n div 16
            set widthNow to widthNow + 1
            -- Finish when n is exhausted and the digit count is a multiple of the resolution. 
            if (n is 0) and (widthNow mod res is 0) then exit repeat
        end repeat
    else
        -- The number's negative.
        set o's hexDigits to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "0"}
        set negatives to "89ABCDEF"

        -- Construct a list of the hexadecimal digits, working backwards from the lowest-order one.
        set widthNow to 0
        repeat
            set c to item (n mod 16 - 1) of o's hexDigits
            set beginning of o's hexOut to c
            set n to (n + 1) div 16 - 1
            set widthNow to widthNow + 1
            -- Finish when n is exhausted, the digit count is a multiple of the resolution,
            -- and the first digit in the list expresses the negative sign bit. 
            if (n is -1) and (widthNow mod res is 0) and (c is in negatives) then exit repeat
        end repeat
    end if

    -- Coerce the digit list to string.
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ""
    set hexOut to o's hexOut as string
    set AppleScript's text item delimiters to astid

    return hexOut
end numToHex