AppleScript: make new quarkxpress document with different master spreads

(*
qxp_document_02.applescript

Creates a new document with two pages with different master spread an some rulers
*)

tell application "QuarkXPress Passport"
    --set default document
    set properties of default document 1 to {class:document, auto leading:"20%", automatic trap amount:"0,144 pt", auto constrain:false, auto picture import:auto import off, automatic text box:false, auto kern:true, kern above:"4 pt", baseline grid increment:"8,9 pt", baseline grid start:"11 mm", baseline grid showing:false, ciceros per centimeter:"2,197", facing pages:false, flex space width:"50%", fractional character widths:true, frame inside:true, guides in front:true, horizontal measure:millimeters, hyphenation method:expanded hyphenation, ignore white:true, item spread coords:false, keep master page items:true, knockout limit:"0", ligatures on:standard ligatures, lock guides:false, maintain leading:true, maximum ligature track:"1", overprint limit:"0", page height:"297 mm", points per inch:"72", process trap:true, page width:"210 mm", subscript horizontal scale:"100%", subscript offset:"33%", subscript vertical scale:"100%", single layout mode:false, trapping method:absolute trap, auto page insertion location:end of story, indeterminate trap amount:"0,144 pt", typesetting leading mode:true, vertical measure:millimeters, bottom margin:"8 mm", column count:1, greek below:"7 pt", greek pictures:false, guides showing:true, gutter width:"4,233 mm", inside margin:"8 mm", invisibles showing:true, left margin:"8 mm", outside margin:"8 mm", right margin:"8 mm", rulers showing:true, top margin:"8 mm", view scale increment:"25%", minimum view scale:"10%", maximum view scale:"800%", view scale:"110,3%"}

    -- neues document
    set thedoc to make new document at beginning

    -- reference zu master document
    set theMaster to object reference of master document 1

    -- neue Musterseiten anlegen
    make new spread at end of master document 1 with properties {name:"left"}
    make new spread at end of master document 1 with properties {name:"right"}

    -- reference zu den musterseiten
    set theLeft to a reference to spread "left" of theMaster
    set theRight to a reference to spread "right" of theMaster

    -- set margin rulers und columns der musterseiten
    set properties of page 1 of theLeft to {column count:1, gutter width:"4,233 mm", top margin:"8 mm", left margin:"8 mm", bottom margin:"8 mm", right margin:"8 mm"}
    set properties of page 1 of theRight to {column count:6, gutter width:"2 mm", top margin:"5 mm", left margin:"5 mm", bottom margin:"5 mm", right margin:"5 mm"}

    -- draw rulers to masterpages
    make new horizontal guide at end of page 1 of theLeft with properties {class:horizontal guide, position:"34 mm", scale:"10%"}
    make new horizontal guide at end of page 1 of theLeft with properties {class:horizontal guide, position:"68,626 mm", scale:"10%"}
    make new horizontal guide at end of page 1 of theLeft with properties {class:horizontal guide, position:"49,509 mm", scale:"10%"}
    make new horizontal guide at end of page 1 of theLeft with properties {class:horizontal guide, position:"78 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theLeft with properties {class:vertical guide, position:"29,566 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theLeft with properties {class:vertical guide, position:"68,957 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theLeft with properties {class:vertical guide, position:"89,232 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theLeft with properties {class:vertical guide, position:"48,683 mm", scale:"10%"}

    make new horizontal guide at end of page 1 of theRight with properties {class:horizontal guide, position:"2,626 mm", scale:"10%"}
    make new horizontal guide at end of page 1 of theRight with properties {class:horizontal guide, position:"36 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theRight with properties {class:vertical guide, position:"13,327 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theRight with properties {class:vertical guide, position:"84 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theRight with properties {class:vertical guide, position:"126 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theRight with properties {class:vertical guide, position:"184,228 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theRight with properties {class:vertical guide, position:"186,737 mm", scale:"10%"}
    make new vertical guide at end of page 1 of theRight with properties {class:vertical guide, position:"189,559 mm", scale:"10%"}

    -- musterseiten zuweisen
    tell thedoc
        set master spread of page 1 to theLeft
        make new page at end of spread 1 with properties {master spread:theRight}
    end tell
end tell

 

AppleScript: make new quarkxpress document

(*
qxp_document_01.applescript

Creates a new document

All properties below are NOT needed to create a new document.
It is possible to use only specific properties, the other properties
will be set to default values.
*)

tell application "QuarkXPress Passport"
    set properties of default document 1 to {class:document, auto leading:"20%", automatic trap amount:"0,144 pt", auto constrain:false, auto picture import:auto import off, automatic text box:false, auto kern:true, kern above:"4 pt", baseline grid increment:"11 pt", baseline grid start:"4 mm", baseline grid showing:false, ciceros per centimeter:"2,197", facing pages:false, flex space width:"50%", fractional character widths:true, frame inside:true, guides in front:true, horizontal measure:millimeters, hyphenation method:expanded hyphenation, ignore white:true, item spread coords:false, keep master page items:true, knockout limit:"0", ligatures on:standard ligatures, lock guides:false, maintain leading:true, maximum ligature track:"1", overprint limit:"0", page height:"297 mm", points per inch:"72", process trap:true, page width:"420 mm", subscript horizontal scale:"100%", subscript offset:"33%", subscript vertical scale:"100%", single layout mode:false, trapping method:absolute trap, auto page insertion location:end of story, indeterminate trap amount:"0,144 pt", typesetting leading mode:true, vertical measure:millimeters, bottom margin:"8 mm", column count:1, greek below:"7 pt", greek pictures:false, guides showing:true, gutter width:"4,233 mm", inside margin:"8 mm", invisibles showing:true, left margin:"8 mm", outside margin:"8 mm", right margin:"8 mm", rulers showing:true, top margin:"8 mm", view scale increment:"25%", minimum view scale:"10%", maximum view scale:"800%", view scale:"86,3%"}
    make new document at beginning
end tell

 

AppleScript: PDF Formularfelder befüllen

Folgendes kleines AppleScript wurde inspiriert durch ein PHP Script.

Ein mit Formularfeldern pröpariertes PDF wird als Vorlage benutzt. Die Formularfelder die als Wert bzw. Standardtext einen Platzhalter enthalten werden durch suchen/ersetzen mit Werten befüllt.

Dieses PDF kann einfach mit den Formularfunktionen von OpenOffice erstellt werden. Hier ist die OO Beispieldatei zu finden.

Einfach und genial. Ein Ansatz mit Potenzial ;-)

(*
pdf_form_fill.applescript

This applescript was inspired by the php script pdf_form_fill from @author Lars Wegmann info@wegmann-it.de

The script replaces the values of the form textfield in a pdf file. 

For example:
Make a new OpenOffice Writer document, insert some form textfields. 
The value of the fields represents the placeholders that would be 
replaced later with applescript.

Notice:
The placeholder it self must have the lenght of the maxlength of the form textfield. If the
textfield has 40 characters the placeholder must have also 40 characters.
The replace string can't be longer than the search string (the placeholder).
*)

--search/replace array
set arr to {{"###FELD1###", "X1"}, {"###FELD2###", "X2"}}

--open pdf file
set pdfdata to readFile("/Applications/MAMP/htdocs/test/PDF/pdf-test.pdf")

--process search/replace array
set pdfdata to processArray(pdfdata, arr)

--write pdf file
writeFile("/Applications/MAMP/htdocs/test/PDF/pdf-test-replaced.pdf", pdfdata)

on readFile(unixPath)
    set foo to (open for access (POSIX file unixPath))
    set txt to (read foo for (get eof foo))
    close access foo
    return txt
end readFile

on writeFile(unixPath, theData)
    set f to open for access unixPath with write permission
    write theData to f as string
    close access f
end writeFile

-- process array of search|replace values
on processArray(theData, arr)
    repeat with sr in arr
        --search replace ascii      
        if item 1 of sr is in theData then
            set theData to str_replace(item 1 of sr, str_pad(item 1 of sr, item 2 of sr), theData)
        end if
        --search replace hex
        if string2hex(item 1 of sr) is in theData then
            set theData to str_replace(string2hex(item 1 of sr), str_pad(string2hex(item 1 of sr), string2hex(item 2 of sr)), theData)
        end if
    end repeat
    return theData
end processArray

-- convert string to hex value
on string2hex(this_String)
    set hex to ""
    repeat with i from 1 to count of this_String
        set hex to hex & char2hex(character i of this_String)
    end repeat
    return hex as string
end string2hex

-- 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

on str_pad(str1, str2)
    set c1 to count of str1
    set c2 to count of str2
    set pad to ""
    if c2 is less than c1 then
        repeat with i from 1 to c1 - c2
            set pad to pad & " "
        end repeat
    end if
    return str2 & pad
end str_pad

-- 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: optimizing loops

(* Loop optimizing Example 1 *)

set MyList to {}
repeat 10000 times
    set the end of MyList to " hello from the loop"
end repeat
return MyList as text
-- 5.63s

set MyString to ""
repeat 10000 times
    set MyString to MyString & " hello from the loop"
end repeat
-- 3.71s
(* Loop optimizing Example 2 for complete overview.
* For speed testing you have to split this example in two separate files
*)

(* 
* slow version
*)
--create a list to work with
set MyList to {}

--put 5000 elements in the list for us to walk through for testing
repeat with i from 0 to 5000
    set the end of MyList to i
end repeat

--now walk the list again and add up all the number
set MyTotal to 0
repeat with ThisValue in MyList
    set MyTotal to MyTotal + ThisValue
end repeat

--return MyTotal
--2.87s

(*
* fast version
*)
--create a global scope list to work with
property MyGlobalList : {}

--make sure it's empty as these are persistent between runs
set MyGlobalList to {}

--now work with a reference to that global list
set MyList2 to a reference to MyGlobalList

--put 5000 elements in the list for us to walk through for testing
repeat with i from 0 to 5000
    set the end of MyList2 to i
end repeat

--now walk the list again and add up all the number
set MyTotal2 to 0
repeat with ThisValue in MyList2
    set MyTotal2 to MyTotal2 + ThisValue
end repeat

--return MyTotal2
--0.09s !!!

 

AppleScript: Worddatei erzeugen und mit Text füllen und speichern

tell application "Microsoft Word"
    activate
    --create new document
    set createDoc to make new document
    set name of font object of text object of createDoc to "Lucida Grande"
    set font size of font object of text object of createDoc to "14"

    --insert some text
    set words of document 1 to "This is a little test with AppleScript."

    --simple text formtting
    select word 4 of createDoc
    set bold of font object of selection to true
    set content of text object of selection to "sunny "

    --appent text to end
    insert text return & "another text" at end of text object of createDoc

    --save file
    save as createDoc file name "MyText.doc"
end tell