Author Topic: Duplicating a Picture Box in Quark  (Read 760 times)

0 Members and 1 Guest are viewing this topic.

Offline mbornbach

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: Duplicating a Picture Box in Quark
« Reply #10 on: October 11, 2016, 05:07:23 PM »
Thanks! :) I got my complicated script working. Here's the full version in case anyone else is looking for something similar:

Code: [Select]
-----------------------------------------------------------------------------------------	
--HOW THIS SCRIPT WORKS
-----------------------------------------------------------------------------------------

--The script first creates a folder named "Publication Backups" on the user's desktop, if that folder doesn't already exist
--Then, the script saves a copy of each pre-prepped file that was dropped on the AppleScript droplet to the "Publication Backups" folder
--Then, the script sets the measurement units in Quark to inches
--Then, the script creates a list of images that are in the Quark document more than once
--After that, the script works with every picture box that is only in the document one time
--(This excludes picture boxes that already have a drop shadow, because those images would have been duplicated :))
----It fits the box to the picture, duplicates the picture box, resizes the duplicate so it is .08" high for most images,
----adds the drop shadow to the duplicate and groups the original picture box with the duplicated picture box.
--Finally, the script displays a dialog for the user when the script is finished prepping files

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------

on open of droppedfiles
with timeout of 1500 seconds -->Changing the timeout to 25 minutes. Default is 2 minutes, which can be way too little.


---------------------------------------------------------------------------------------------------
--CREATING BACKUP FOLDER IF IT DOESN'T EXIST
---------------------------------------------------------------------------------------------------

--Setting the path to the desktop
tell application "Finder"
set desktopFolder to path to desktop as string
end tell


--Creating the Publication Backups folder if it doesn't exist yet
if not my CheckForFolder(desktopFolder & "Publication Backups:") then
tell application "Finder"
make new folder at folder desktopFolder with properties {name:"Publication Backups"}
end tell
end if

--Setting the publication_backup folder to the folder named Publication Backups on the desktop
set publication_backup to desktopFolder & "Publication Backups:"

---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------------
--MAKING A BACKUP COPY OF EACH FILE BEFORE PROCESSING
---------------------------------------------------------------------------------------------------

--Starting the repeat loop
repeat with afile in droppedfiles


--Making a backup copy of each file to the desktop before processing
tell application "Finder"
activate
duplicate afile to publication_backup with replacing
end tell

---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------
--OPENING THE QUARK FILE & SETTING THE MEASUREMENTS TO INCHES
-----------------------------------------------------------------------------------------

tell application "QuarkXPress"
activate


--Opening the file with warning dialogs suppressed
open afile as alias with Suppress All Warnings


tell document 1

set vertical measure to inches
set horizontal measure to inches

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------
--BUILDING A LIST OF THE IMAGES THAT ARE IN THE DOCUMENT MORE THAN ONCE
-----------------------------------------------------------------------------------------

set originalList to {}

--Looping through every graphic to build a list that contains the name of every link
set theBoxes to every picture box
repeat with abox in theBoxes
set thepath to file path of image 1 of abox as string
set originalList to originalList & thepath
end repeat

--------------------------

--Building two lists of every link that has at least 2 copies in the document
set list1 to {}
--display dialog aLinkName & "-" & matchCount
repeat with x from 1 to count of items of originalList
set n to item x of originalList
--if n is in list2 and n is not in doubleImageList then set end of doubleImageList to n
set matchCount to my count_matches(originalList, n)
if matchCount is greater than 1 then
set list1 to list1 & n
end if
end repeat
--returns {AG123456.tif, AG123456.tif}

set list2 to {}
repeat with x from 1 to count of items of originalList
set n to item x of originalList
set matchCount to my count_matches(originalList, n)
if matchCount is greater than 1 then
set list2 to list2 & n
end if
end repeat
--returns {AG123456.tif, AG123456.tif}

--------------------------

--Building final list which only contains the name of duplicate images once
set doubleImageList to {}
repeat with x from 1 to count of items of list1
set n to item x of list1
if n is in list2 and n is not in doubleImageList then set end of doubleImageList to n
end repeat
--returns {AG123456.tif}

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------
--CREATING A BLANK LIST FOR ALL THE BOXES THAT SHOULD HAVE A FULL SHADOW INSTEAD OF A TINY ONE AT THE BOTTOM
--This list will have items added to it as we loop through the images
-----------------------------------------------------------------------------------------

set full_shadow_image_list to {}

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------
--ADDING ALL PICTURE BOXES THAT SHOULD BE SHADOWED TO A LIST
-----------------------------------------------------------------------------------------

--Looping through each picture box to see if it is in the document more than once
set pictureBoxes to every picture box
set shadowedIDList to {}
set unshadowedIDList to {}
repeat with i from 1 to the number of pictureBoxes

set pictureBox to item i of pictureBoxes
set imagePath to file path of image 1 of pictureBox as string


--Getting the bounds (coordinates) of the picture box
set y1_orig to coerce (item 1 of (bounds of pictureBox as list)) to real
set x1_orig to coerce (item 2 of (bounds of pictureBox as list)) to real
set y2_orig to coerce (item 3 of (bounds of pictureBox as list)) to real
set x2_orig to coerce (item 4 of (bounds of pictureBox as list)) to real


--Doing some math to determine the square inches of the picture box and image
set unshadowedBox_Height_orig to (y2_orig - y1_orig)
set unshadowedBox_Width_orig to (x2_orig - x1_orig)
set unshadowedBox_SquareInches to (unshadowedBox_Height_orig * unshadowedBox_Width_orig)


--Adding the box to the list if it is not in the document twice, it is in the 1_4cIMAGES folder and it has a blain number within the filename
if imagePath is not in doubleImageList and imagePath contains "1_4c IMAGES" and imagePath does not contain "Lifestyle" and imagePath does not contain "lifestyle" and imagePath does not contain ".eps" and imagePath does not contain ".ai" then
set unshadowedIDList to unshadowedIDList & uniqueID of pictureBox as string


--Moving the picture box to the front if it contains a logo
else if imagePath contains "2_LOGOS" then
move pictureBox to the front
end if

-----------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------
--ADDING ALL PICTURE BOXES THAT SHOULD HAVE A FULL SHADOW TO A LIST
--(INSTEAD OF A TINY SHADOW AT THE BOTTOM)
-----------------------------------------------------------------------------------------

--Making sure that the tiny color swatches for clothing & sporting goods don't get shadowed
if imagePath contains "CL" and unshadowedBox_SquareInches is less than 1 then
set shadowBox to false
else if imagePath contains "SG" and unshadowedBox_SquareInches is less than 1 then
set shadowBox to false
else
set shadowBox to true
end if


--Adding the items that will get a shadow behind most of the image to a list
--(This would be items like shirts, jackets, sweaters that need a taller shadow)
set full_shadow_list to {"alfred", "Alfred", "bioworld", "bio world", "Bioworld", "Bio world", "BioWorld", "Bio World", "california", "California", "cgcg", "CgCg", "Cgcg", "CGCG", "columbia", "Columbia", "daniel", "Daniel", "erika", "Erika", "studioRay", "Studioray", "StudioRay", "TLBHolding", "TLB Holding", "tlb holding", "tlbholding", "TLBholding"}

repeat with full_shadow_item in full_shadow_list
if imagePath contains (contents of full_shadow_item) then set end of full_shadow_image_list to (imagePath as text)
end repeat

end repeat

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------
--FITTING BOX TO PICTURE, UNGROUPING BOXES THAT SHOULD BE SHADOWED,
--DUPLICATING THE BOX, ADDING A DROP SHADOW TO THE DUPLICATED BOX
--RESIZING THE DUPLICATED BOX TO .08" HIGH AND GROUPING THE ORIGINAL BOX WITH THE DUPLICATE
-----------------------------------------------------------------------------------------

--Looping through the list of boxes that need to be duplicated and drop shadowed
set unshadowedBoxes to (every picture box whose uniqueID is in unshadowedIDList)
repeat with i from (number of unshadowedBoxes) to 1 by -1
set unshadowedBox to item i of unshadowedBoxes
set unshadowed_image_path to file path of image 1 of unshadowedBox as string


--Ungrouping boxes that will need a shadow
try
set group_boxes to every group box
repeat with group_box in group_boxes
if unshadowed_image_path of group_box is not in doubleImageList then
repeat until (count of group_boxes) is 0
tell group_box
set grouped to false
end tell
end repeat
end if
end repeat
end try

--------------------

--Getting the bounds (coordinates) of the picture box
set y1 to coerce (item 1 of (bounds of unshadowedBox as list)) to real
set x1 to coerce (item 2 of (bounds of unshadowedBox as list)) to real
set y2 to coerce (item 3 of (bounds of unshadowedBox as list)) to real
set x2 to coerce (item 4 of (bounds of unshadowedBox as list)) to real


--Getting the bounds (coordinates) of the image inside the picture box
set y1_image to coerce (item 1 of (actual bounds of image 1 of unshadowedBox as list)) to real
set x1_image to coerce (item 2 of (actual bounds of image 1 of unshadowedBox as list)) to real
set y2_image to coerce (item 3 of (actual bounds of image 1 of unshadowedBox as list)) to real
set x2_image to coerce (item 4 of (actual bounds of image 1 of unshadowedBox as list)) to real

--------------------

--Doing some math to determine the square inches of the picture box and image
set unshadowedBox_Height to (y2 - y1)
set unshadowedBox_Width to (x2 - x1)
set unshadowedBox_SquareInches to (unshadowedBox_Height * unshadowedBox_Width)
set unshadowedImage_Height to (y2_image - y1_image)
set unshadowedImage_Width to (x2_image - x1_image)
set unshadowedImage_SquareInches to (unshadowedImage_Height * unshadowedImage_Width)

--------------------

--Fitting the box to the picture if the picture is smaller, equal to or very slightly bigger than the picture box
if unshadowedImage_SquareInches is less than (unshadowedBox_SquareInches * 1.1) then

select unshadowedBox

try

--Pressing Command Shift C to activate the fit box to picture keyboard shortcut
tell application "System Events" to key code 8 using {shift down, command down}

on error

--Selecting "Fit Box To Picture" in the quark menu if the user does not have the fit box to picture keyboard shortcut
my do_submenu("QuarkXPress", "Style", "Fit Box To Picture")
end try

end if


--Unselecting the picture box
set selection to null

--------------------

--Getting the bounds (coordinates) of the picture box, now that it has been fit to the picture
set y1_final to coerce (item 1 of (bounds of unshadowedBox as list)) to real
set x1_final to coerce (item 2 of (bounds of unshadowedBox as list)) to real
set y2_final to coerce (item 3 of (bounds of unshadowedBox as list)) to real
set x2_final to coerce (item 4 of (bounds of unshadowedBox as list)) to real


--Getting the bounds (coordinates) of the image inside the picture box, now that it has been fit to the picture
set y1_image_final to coerce (item 1 of (actual bounds of image 1 of unshadowedBox as list)) to real
set x1_image_final to coerce (item 2 of (actual bounds of image 1 of unshadowedBox as list)) to real
set y2_image_final to coerce (item 3 of (actual bounds of image 1 of unshadowedBox as list)) to real
set x2_image_final to coerce (item 4 of (actual bounds of image 1 of unshadowedBox as list)) to real

--------------------

--Duplicating the picture box
set duplicateBox to duplicate unshadowedBox to after unshadowedBox

--------------------

--Getting the offset of the duplicate box
set props to properties of image 1 of duplicateBox
set {off1, off2} to (coerce offset of props to list)
set {Ys, Xs} to {coerce off1 to real, coerce off2 to real}


--------------------

--Checking if the picture box is rotated
set myProps to properties of duplicateBox
set myRotation to coerce (rotation of myProps) to string


--Making the duplicated shadowed box only .08" high
--unless the file path of the image contains certain vendor names
if text 1 of myRotation is "0" then
if unshadowed_image_path is not in full_shadow_image_list and unshadowed_image_path does not contain "FT" then

set new_y1_final to y2_final - 0.08 as number
set bounds of duplicateBox to {new_y1_final, x1_final, y2_final, x2_final}


--Doing some math to figure out where to move the image back to
--because Quark is being a pain and insists on moving the image with the box
set unshadowedBox_Height_final to (y2_final - y1_final)
set new_Ys to Ys - (unshadowedBox_Height_final - 0.08)
try
set offset of image 1 of duplicateBox to {new_Ys, Xs}
end try


--Making the duplicated shadowed box 0.35" tall if the path contains "FT"
else if unshadowed_image_path is not in full_shadow_image_list and unshadowed_image_path contains "FT" then

set new_y1_final to y2_final - 0.35 as number
set bounds of duplicateBox to {new_y1_final, x1_final, y2_final, x2_final}


--Doing some math to figure out where to move the image back to
--because Quark is being a pain and insists on moving the image with the box
set unshadowedBox_Height_final to (y2_final - y1_final)
set new_Ys to Ys - (unshadowedBox_Height_final - 0.35)
try
set offset of image 1 of duplicateBox to {new_Ys, Xs}
end try



--Making the duplicated shadowed box behind most of the image
--if the image path contains certain vendor names
else if unshadowed_image_path is in full_shadow_image_list then
set new_y1_final to y1_final + 0.1 as number
set bounds of duplicateBox to {new_y1_final, x1_final, y2_final, x2_final}


--Doing some math to figure out where to move the image back to
--because Quark is being a pain and insists on moving the image with the box
set unshadowedBox_Height_final to (y2_final - y1_final)
set new_Ys to -0.1
try
set offset of image 1 of duplicateBox to {new_Ys, Xs}
end try
end if
end if

--------------------

--This fixes the bug that opens the usage, missing font and replace font windows
tell application "System Events"
tell process "QuarkXPress"
set myUI to every UI element
try
if name of window 1 contains "Usage" then tell application "System Events" to key code 53
if name of window 1 contains "Font" then tell application "System Events" to key code 53
end try
end tell
end tell

--------------------

--The magic that adds the drop shadow
select duplicateBox
tell application "System Events" to key code 97 using {command down} -->Pressing Command F6 to activate the drop shadow item style
set selection to null

--------------------

--This fixes the bug that opens the usage, missing font and replace font windows again in case that window pops up twice
tell application "System Events"
tell process "QuarkXPress"
set myUI to every UI element
try
if name of window 1 contains "Usage" then tell application "System Events" to key code 53
if name of window 1 contains "Font" then tell application "System Events" to key code 53
end try
end tell
end tell

--------------------

--Grouping the original picture box with the duplicate box
try
set tool mode to drag mode
end try

set selection to null

set selected of duplicateBox to true
set selected of unshadowedBox to true
set grouped of group box 1 to true
set selection to null


end repeat


-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------
--MOVING EVERY TEXT BOX THAT HAS A TRANSPARENT BACKGROUND TO THE FRONT
-----------------------------------------------------------------------------------------

set bgColor to null
set noColor to "None"

set textBoxes to every text box
repeat with i from 1 to number of textBoxes
set thisTextBox to item i of textBoxes
set textBoxProperties to properties of thisTextBox
set textBoxColor to color of textBoxProperties
if textBoxColor is "None" or textBoxColor is null or textBoxColor is "null" then
move thisTextBox to the front
end if
end repeat

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------




-----------------------------------------------------------------------------------------
--SAVING & CLOSING THE DOCUMENTS
-----------------------------------------------------------------------------------------

close saving yes

end tell --end of tell document 1 block
end tell --end of tell Quark block
end repeat --end of first repeat loop

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------
--DISPLAYING A DIALOG TO THE USER THAT THE PAGES ARE DONE
-----------------------------------------------------------------------------------------

tell application "SystemUIServer" to display dialog "The pages are finished prepping." & return & return & "If you need to access the pre-prepped document, you will find it in the Publication Backups folder on your desktop." giving up after 4500 with icon note
end timeout
end open

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------


-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------

--Function to check if a folder exists
on CheckForFolder(thisFolder)
tell application "Finder"
return (exists folder thisFolder)
end tell
end CheckForFolder


--Function that counts the number of matches in a list
on count_matches(this_list, this_item)
set the match_counter to 0
repeat with i from 1 to the count of this_list
if item i of this_list is this_item then
set the match_counter to the match_counter + 1
end repeat
return the match_counter
end count_matches


--The UI Scripting select submenu function
--Thanks to http://www.macosxautomation.com/applescript/uiscripting/
on do_submenu(app_name, menu_name, menu_item)
try
-- bring the target application to the front
tell application app_name
activate
end tell
tell application "System Events"
tell process app_name
tell menu bar 1
pick menu bar item menu_name
tell menu bar item menu_name
tell menu menu_name
pick menu item menu_item
click menu item menu_item
end tell
end tell
end tell
end tell
end tell
return true
on error error_message
return false
end try
end do_submenu