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

 

AppleScript: Javascript in Safari ausführen

(*
force safari to load url. if url is loaded use javascript to submit form.
*)
tell application "Safari" to open location "http://www.heise.de"
if my page_loaded(20) is true then
    tell application "Safari"
        activate
        tell document 1
            do JavaScript "document.getElementById('suchfeld').q.value='Applescript'; 
document.getElementById('suchfeld').submit()"
            if my page_loaded(20) is true then
                do JavaScript "var b = new Array(); 
for (var i=0;i<document.getelementbyid('filter').getelementsbytagname('a').length;i++) if="" documentgetelementbyid="" filter="" getelementsbytagname="" a="" iinnerhtmlindexof="" ct="">-1) 
{ 
window.location.href = document.getElementById('filter').getElementsByTagName('a')[i].getAttribute('href') 
break; 
} 
} 
"
            end if
        end tell
    end tell
else
    display dialog "There was a timeout"
end if

on page_loaded(timeout_value)
    set i to 0
    delay 1
    tell application "Safari"
        repeat until (do JavaScript "document.readyState" in document 1) is "complete"
            delay 0.5
            if i > timeout_value then return false
            set i to i + 1
        end repeat
        return true
    end tell
end page_loaded
</document.getelementbyid('filter').getelementsbytagname('a').length;i++)>

 

AppleScript: quarkxpress export all layouts as pdf

set TargetFolder to choose folder with prompt "Please choose the folder where to save the PDFs"
tell application "QuarkXPress"
    set MyProject to object reference of project 1
    tell MyProject
        repeat with i from 1 to count of layout space
            try
                export layout space i as "PDF" in ((TargetFolder as string) & name of layout space i & ".pdf")
            on error errMsg number errNum
            end try
        end repeat
    end tell
end tell