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

 

Rename and Relinking Images in QuarkXPress using AppleScript

set piclist to {}
set datlist to {}
global missed
set missed to {}
global dat, datlist, fileID
global textFile, available
set SourceFile to (choose file with prompt "Choose any quark document file...")
set textFile to (choose file with prompt "Choose text file ...")
set thepath to choose folder with prompt "Select folder to images in..."

tell application "Finder"
    if (file textFile exists) then
        set fileID to open for access textFile
        try
            set myfilecontents to read textFile
        on error
            display dialog "Check the text file..."
            return
        end try
        close access textFile
    end if
end tell

tell application "QuarkXPress"
    try
        open SourceFile use doc prefs yes remap fonts no do auto picture import no
    on error
        display dialog "check the file is already opened..."
        return
    end try

    tell document 1
        set pb to count of images

        set the Tmppara_list to every paragraph of myfilecontents
        repeat with i from 1 to count of paragraph of myfilecontents
            set Tmpln to i
            set Tmp_p1 to paragraph i of myfilecontents
            set TmpTextpara to Tmp_p1
            set prevTIDs to text item delimiters of AppleScript
            set text item delimiters of AppleScript to tab
            --set subject to text items of "A   B"
            set subject to text items of TmpTextpara
            set srcImg to item 1 of subject
            set desImg to item 2 of subject
            set text item delimiters of AppleScript to prevTIDs
            --end repeat
            set available to false
            repeat with i from 1 to count of images
                tell image i
                    set ImageBox to object reference of picture box 1
                    -- Copy writable properties
                    set Img_file_path to (file path) as text
                    if (Img_file_path ? "null" and Img_file_path ? "no disk file") then
                        set theImageName to my NameFromPath(Img_file_path)
                        if (theImageName ? "null" and theImageName ? "no disk file") then
                            if theImageName = srcImg then
                                set available to true
                                if not missing then
                                    set Img_angle to angle
                                    set Img_color to color
                                    set Img_greek_pictures to greek pictures
                                    set Img_image_trap to image trap
                                    set Img_invert_runaround to invert runaround
                                    --set Img_model to model
                                    set Img_name to name
                                    --set Img_negative to negative
                                    set Img_offset to offset
                                    set Img_scale to scale
                                    set Img_screen to screen
                                    set Img_screen_angle to screen angle
                                    set Img_screen_frequency to screen frequency
                                    set Img_screen_function to screen function
                                    set Img_shade to shade
                                    set Img_show_half_tone to show half tone
                                    set Img_skew to skew
                                    set Img_suppress_printing to suppress printing

                                    set Img_file_path to (file path) as text

                                    tell application "Finder"
                                        try
                                            set the datlist to files of thepath
                                        end try
                                        repeat with dat in datlist
                                            set fname to name of dat as string
                                            if fname = srcImg then
                                                set name of dat to desImg
                                                exit repeat
                                            end if
                                        end repeat
                                    end tell

                                    -- Get new image path
                                    set Img_file_path to (file path) as text
                                    set Img_file_path to my FindReplace(srcImg, desImg, Img_file_path)

                                    -- Re-import picture
                                    try
                                        set image 1 of ImageBox to file Img_file_path

                                        -- Re-set image properties
                                        tell image 1 of ImageBox
                                            set angle to Img_angle
                                            set color to name of Img_color
                                            set greek pictures to Img_greek_pictures
                                            set image trap to Img_image_trap
                                            set invert runaround to Img_invert_runaround
                                            --set model to Img_model
                                            set name to Img_name
                                            --set negative to Img_negative
                                            set offset to Img_offset
                                            set scale to Img_scale
                                            set screen to Img_screen
                                            set screen angle to Img_screen_angle
                                            try
                                                set screen frequency to Img_screen_frequency
                                            end try
                                            set screen function to Img_screen_function
                                            set shade to Img_shade
                                            set show half tone to Img_show_half_tone
                                            set skew to Img_skew
                                            set suppress printing to Img_suppress_printing
                                        end tell
                                    end try
                                end if
                                --else
                                --available = false
                            end if
                        end if
                    end if
                end tell

            end repeat
            if available = false then
                --display dialog srcImg
                set missed to missed & srcImg & return
            end if
        end repeat
    end tell
    save document 1
    --copy (choose file name default name "missed" & ".txt") as string to dataFile
    set SourceFile to SourceFile as string
    set dataFile to my FindReplace(".qxd", ".txt", SourceFile)
    try
        open for access file (dataFile) with write permission
        set eof of file (dataFile) to 0
        write item 1 of missed to file (dataFile) starting at 0
        repeat with i from 2 to length of missed
            try
                write item i of missed to file (dataFile)
            end try
        end repeat
        close access file (dataFile)
    on error errtext number errNum
        close access file (dataFile)
        tell application "QuarkXPress"
            activate
            --          display dialog errtext
            display dialog "No missed files..."
        end tell
    end try

    display dialog "completed"
    return
end tell

on FindReplace(FindWhat, ReplaceBy, ThisString)
    copy the text item delimiters to OldDelims
    set the text item delimiters to {FindWhat}
    set TempList to every text item of ThisString
    set the text item delimiters to {ReplaceBy}
    set NewString to TempList as text
    set the text item delimiters to OldDelims
    return NewString
end FindReplace

on NameFromPath(theImagePath)
    set theOffset to the offset of ":" in (the reverse of every character of theImagePath) as string
    set theImageName to (characters -(theOffset - 1) thru -1 of theImagePath) as string
    return theImageName
end NameFromPath

 

AppleScript: quarkxpress version droplet

(*
this in a older examples that i found in the web...
*)

on open fileList
  repeat with aDoc in fileList
    set filePath to aDoc as string
    if filePath ends with ":" then
    else
      set fileInfo to (info for file filePath)
      if file type of fileInfo is in {"XDOC", "XPRT"} then
        set docName to name of fileInfo
        set docVers to getVers(filePath)
        display dialog "Die Version des Dokuments " & docName & ": " & docVers & "." buttons "OK" default button 1 with icon 1
      end if
    end if
  end repeat
end open

on getVers(filePath)
  set versionClue to read file filePath for 12
  if versionClue contains "MM" then
    if versionClue ends with "?" then
      return "QuarkXPress 3.x (Mac)"
    else if versionClue ends with "A" then
      return "QuarkXPress 4.x (Mac)"
    else if versionClue ends with "B" then
      return "QuarkXPress 5.x (Mac)"
    else if versionClue ends with "C" then
      return "QuarkXPress 6.x (Mac)"
    else
      return "unbekannt (Mac)"
    end if
  else if versionClue contains "II" then
    set testChar to character 11 of versionClue
    if character 11 of versionClue = "?" then
      return "QuarkXPress 3.x (Windows)"
    else if character 11 of versionClue = "A" then
      return "QuarkXPress 4.x (Windows)"
    else if character 11 of versionClue = "B" then
      return "QuarkXPress 5.x (Windows)"
    else if character 11 of versionClue = "C" then
      return "QuarkXPress 6.x (Windows)"
    else
      return "unbekannt (Windows)"
    end if
  end if
end getVers