Schlagwort-Archive: applescript

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

 

AppleScript: hex 2 num konvertieren

on hexToNum from h given sign:sign
    script o
        property hexChrs : "0123456789abcdef" as Unicode text -- TIDs obey AppleScript's case attribute with Unicode.

        on getHexVal(c)
            set AppleScript's text item delimiters to c
            set v to (count text item 1 of hexChrs)
            if (v is 16) then error "Non-hex character(s)."

            return v
        end getHexVal
    end script

    set astid to AppleScript's text item delimiters
    try
        set h to h as Unicode text -- Speeds up use of TIDs with Unicode 'hexChrs' in o.
        tell o to set n to getHexVal(character 1 of h)
        if (sign) and (n > 7) then set n to n - 16

        repeat with i from 2 to (count h)
            tell o to set n to n * 16 + getHexVal(character i of h)
        end repeat
    on error msg number errNum
        set AppleScript's text item delimiters to astid
        error "hexToNum handler: " & msg number errNum
    end try
    set AppleScript's text item delimiters to astid

    return n
end hexToNum

 

AppleScript: char 2 hex konvertieren

-- convert character to hex value
on char2hex(this_char)
  set the ASCII_num to (the ASCII number this_char)
  set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
  set x to item ((ASCII_num div 16) + 1) of the hex_list
  set y to item ((ASCII_num mod 16) + 1) of the hex_list
  return ("00" & x & y) as string
end char2hex

 

AppleScript: Text suchen/ersetzen advanced

-- find : Text (or list of text) to be found
-- replace : Text (or list of text) to replace with
-- subject : Text (or list of text) to be searched
on str_replace(find, replace, subject)
  set prevTIDs to text item delimiters of AppleScript
  set returnList to true

  -- This wouldn't make sense (you could have it raise an error instead)
  if class of find is not list and class of replace is list then return subject

  if class of find is not list then set find to {find}
  if class of subject is not list then ¬
    set {subject, returnList} to {{subject}, false}

  set findCount to count find
  set usingReplaceList to class of replace is list

  try
    repeat with i from 1 to (count subject)
      set thisSubject to item i of subject
      repeat with n from 1 to findCount
        set text item delimiters of AppleScript to item n of find
        set thisSubject to text items of thisSubject
        if usingReplaceList then
          try
            item n of replace
          on error
            "" -- `replace` ran out of items
          end try
        else
          replace
        end if

        set text item delimiters of AppleScript to result
        set thisSubject to "" & thisSubject
      end repeat

      set item i of subject to thisSubject
    end repeat
  end try

  set text item delimiters of AppleScript to prevTIDs
  if not returnList then return beginning of subject
  return subject
end str_replace

 

AppleScript: Text suchen/ersetzen

display dialog( my searchAndReplace("my mommy", "my", "your") )

on searchAndReplace( txt, srch, rpl )
    set oldtid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {srch}
    set temp to every text item of txt
    set AppleScript's text item delimiters to {rpl}
    set temp to (temp as string)
    set AppleScript's text item delimiters to oldtid
    return temp
end searchAndReplace