applescript Illustrator scaling based on return dialog - applescript

I'm working on a script to eliminate repetition.What I have so far will obtain the current size of the art in Illustrator and display the size in inches but I'm having trouble figuring out how to scale the art based on a return dialog. I'm trying to get it to scale the art proportionally to the measurement returned for the width. It will then resize the artboard based on the new size of the art (this part works). Here's what I have so far
tell application "Adobe Illustrator"
activate
tell document 1
--define Spot1
set docColorSpace to color space
if (docColorSpace is CMYK) then
set SpotColor1 to {cyan:21.0, magenta:0, yellow:100.0, black:0.0}
else
set SpotColor1 to {red:206.0, green:219.0, blue:41.0}
end if
--color change first
set (path items whose fill color is not SpotColor1)'s fill color to SpotColor1
--get current size
set tempGroup to make new group item with properties {name:"TempGroup"} -- Create a temporary container group
duplicate every page item to beginning of group item "TempGroup" -- copy instances to it
set {w, h} to {width, height} of tempGroup -- get properties
delete tempGroup
display dialog "Width of Art: " & (w / 72) & return & "Height of Art: " & (h / 72)
--change to desired size PROBLEM SECTION
set ArtWidth to text returned of (display dialog "Please enter Art Width:" default answer "10")
try
if (w / 72) is greater than ArtWidth then
set sizeDifference to (ArtWidth - (w / 72)) as integer
else
if (w / 72) is less than ArtWidth then
set sizeDifference to ((w / 72) - ArtWidth) as integer
end if
end if
set has selected artwork of (every layer of document 1) to true
set everyPageItem to artItem
set x1orig to item 1 of activeArt
set y1orig to item 2 of activeArt
set x2orig to item 3 of activeArt
set y2orig to item 4 of activeArt
--not sure if activeArt
--determine new page size (add inch in each direction)
set x1new to (x1orig - sizeDifference) as real
set y1new to (y1orig + sizeDifference) as real
set x2new to (x2orig + sizeDifference) as real
set y2new to (y2orig - sizeDifference) as real
--set new art size
set artItem to {x1new, y1new, x2new, y2new}
--fit artboard to art
set tempGroup to make new group item with properties {name:"TempGroup"} -- Create a temporary container group
duplicate every page item to beginning of group item "TempGroup" -- copy instances to it
set {w, h} to {width, height} of tempGroup -- get properties
set theBounds to visible bounds of tempGroup
set artboard rectangle of first artboard to theBounds
delete tempGroup
-- one inch border
tell artboard 1
--get original page size
set artPage to artboard rectangle
set x1orig to item 1 of artPage
set y1orig to item 2 of artPage
set x2orig to item 3 of artPage
set y2orig to item 4 of artPage
--determine new page size (add inch in each direction)
set x1new to (x1orig - 72) as real
set y1new to (y1orig + 72) as real
set x2new to (x2orig + 72) as real
set y2new to (y2orig - 72) as real
--set new page size
set artboard rectangle to {x1new, y1new, x2new, y2new}
--print to rip, set path to folder
--hop out to finder, close & file folders where they go
end tell
end try
end tell
end tell
end
I'm feel like I'm overlooking an easier way to do this - any suggestions appreciated

This is taken from a subroutine I use for scaling art to a specific width. Notice that the difference in inches doesn't actually matter. You just need to know the scale between the original size and the new size.
set newWidth to text returned of (display dialog "Please enter Art Width:" default answer "10")
tell application "Adobe Illustrator"
set artWidth to width of tempGroup
set scalePercentage to (newWidth / artWidth) * 100
scale tempGroup horizontal scale scalePercentage vertical scale scalePercentage
end tell

The problem with the script was Illustrator works with points rather than inches so I had to convert the points to inches like so
tell application "Adobe Illustrator"
activate
tell document 1
--get current size
set tempGroup to make new group item with properties {name:"TempGroup"} -- Create a temporary container group
duplicate every page item to beginning of group item "TempGroup" -- copy instances to it
set {w, h} to {width, height} of tempGroup -- get properties
delete tempGroup
display dialog "Width of Art: " & (w / 72) & return & "Height of Art: " & (h / 72)
--resize
set newWidth to text returned of (display dialog "Please enter Art Width:" default answer "10")
set artWidth to width of tempGroup
set sum to (artWidth / newWidth * 100)
set scalePercentage to (72 / sum * 10000)
scale tempGroup horizontal scale scalePercentage vertical scale scalePercentage
end tell
end tell
end

Related

Show context menu on Mac with keyboard using AppleScript and Automator

I am trying to find a way to bring up the context menu in Finder on a Mac with Yosemite without touching the mouse/touchpad.
A context menu.
After extensive research on this issue, the only possible route seems to be using AppleScript with Automator, and assign keyboard shortcut to it.
The AppleScript below was found on stackoverflow, if I run it inside the Automator, it would bring up the context menu on one of the files on the desktop (not the file currently selected.)
tell application "System Events"
tell process "Finder"
set target_index to 1
set target to image target_index of group 1 of scroll area 1
tell target to perform action "AXShowMenu"
end tell
end tell
Automator screenshot
But I am having trouble getting it to work with keyboard shortcut.
Also I will need to make sure that it brings the menu for the currently selected file.
Can someone provide some insight about how this can be done?
You can read about the script below here: MacScripter / right click
# Copyright © 2012 - 2015 McUsr
run showRightClickMenu
script showRightClickMenu
on run
set mouseLoc to (do shell script "~/opt/bin/MouseTools -location")
set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
tell mouseLoc to set {mouseX, mouseY} to {it's text item 1, it's text item 2}
set {mouseX, mouseY} to {(mouseX as integer), 1200 - (mouseY as integer)}
tell application id "sevs"
set frontProcessName to name of every process whose frontmost is true
-- tell a to set aa to (get its name)
set wnCount to count of windows of process named frontProcessName
if wnCount > 0 then
tell window 1 of process named frontProcessName
set wnPos to its position
set wnsize to its size
end tell
set {wnX, wnY, wnWidth, wnHeight} to {item 1 of wnPos, item 2 of wnPos, item 1 of wnsize, item 2 of wnsize}
set {leftBound, RightBound, upperBound, lowerBound} to {wnX + 1, (wnX + wnWidth - 21), wnY + 50, (wnY + wnHeight - 51)}
if mouseX ≥ leftBound and mouseX ≤ RightBound then
else if mouseX < leftBound then
set mouseX to leftBound
else
set mouseX to RightBound
end if
if mouseY ≥ upperBound and mouseY ≤ lowerBound then
else if mouseY < upperBound then
set mouseY to upperBound
else
set mouseY to lowerBound
end if
end if
end tell
set mouseLoc to "c" & mouseX & " " & mouseY
do shell script "~/opt/bin/cliclick " & mouseLoc
set AppleScript's text item delimiters to astid
end run
end script
This will bring up the context menu of the currently selected file of the desktop:
tell application "Finder"
set sel to get the selection
if (sel is {}) then
log "Nothing selected! Can't proceed"
return
end if
set target_item_name to the name of (item 1 of sel)
end tell
tell application "System Events"
tell process "Finder"
tell group 1 of scroll area 1
set target to the first image whose value of attribute "AXFilename" is target_item_name
tell target to perform action "AXShowMenu"
end tell
end tell
end tell
*Tested on 10.8.5 in script editor

Multiple conditional using AppleScript in Indesign -- won't work?

This is making me lose my mind. Here's a short version of thee code that I'm trying to get to work. It's returning bizarre values and giving me errors at random height and width settings. I can't for the life of me figure out where I went wrong! I thought my logic gate to determine the inputs was rock solid! Any help would be greatly appreciated!
tell application "Adobe InDesign CS6"
activate
set myDoc to active document
set origLevel to user interaction level of script preferences
set user interaction level of script preferences to interact with all
set myDialog to make dialog with properties {name:"Make Template", can cancel:true}
tell myDialog
tell (make dialog column)
tell (make border panel)
tell (make dialog column)
make static text with properties {static label:"Width:", min width:60}
make static text with properties {static label:"Height:", min width:60}
make static text with properties {static label:"Bleed:", min width:60}
end tell
tell (make dialog column)
set myWidth to make text editboxes with properties {edit contents:"", min width:60}
set myHeight to make text editboxes with properties {edit contents:"", min width:60}
set myBleed to make text editboxes with properties {edit contents:"", min width:60}
end tell
tell (make dialog column)
make static text with properties {static label:"in", min width:0}
make static text with properties {static label:"in", min width:0}
make static text with properties {static label:"in", min width:0}
end tell
tell (make dialog column)
make static text with properties {static label:"", min width:25}
end tell
end tell
end tell
end tell
set userResponse to show myDialog
if userResponse is true then
set docWidth to edit contents of myWidth as string
set docHeight to edit contents of myHeight as string
set docBleed to edit contents of myBleed as string
destroy myDialog
else
destroy myDialog
error number -128
end if
tell myDoc
if docHeight > docWidth then
set bigDim to docHeight
else
set bigDim to docWidth
end if
if bigDim ≤ 216 then
set buildSize to "1"
else if bigDim > 216 and bigDim ≤ 432 then
set buildSize to "2"
else if bigDim > 432 and bigDim ≤ 864 then
set buildSize to "4"
else if bigDim > 864 and bigDim ≤ 2160 then
set buildSize to "10"
end if
set newWidth to (docWidth / buildSize)
set newHeight to (docHeight / buildSize)
set newBleed to (docBleed / buildSize)
set document bleed top offset of document preferences to newBleed
set page width of document preferences to newWidth
set page height of document preferences to newHeight
end tell
set user interaction level of script preferences to origLevel
end tell
You write
set docWidth to edit contents of myWidth as string
set docHeight to edit contents of myHeight as string
and in fact you compare a string with an integer: if bigDim ≤ 216 then. To handle your code Applescript has to convert one of these values and it looks like it converts the value 216 to a string "216". Using string comparison the string "5" is greater than "216" and fits the comparison else if bigDim > 432 and bigDim ≤ 864 then, because the string "5" fits between "432" and "864".
What about converting the edit contents to integer?
set docWidth to edit contents of myWidth as integer
set docHeight to edit contents of myHeight as integer
BTW the used code set newWidth to (docWidth / buildSize) later in your script worked only because Applescript is clever enough to convert both values to numbers because it just don't make sense to divide two strings ;-)
Enjoy, Michael / Hamburg

Applescript Illustrator scale art to returned dialog

The first part of this script finds the art size and returns with the size in inches -
I want this to then ask what size to make the width then enter the number and have Illustrator scale the art to the width in the returned dialog.
I'm trying to figure out where the calculation is off here.
It seemed at first the scale percentage would be correct but to enlarge the
percentage must be over 100 % (i.e. 125% or 150% etc.)
I can't get the script to compile with an else between my if's so I'm sure I'm missing something here.
tell application "Adobe Illustrator"
activate
tell document 1
--define Spot1
set docColorSpace to color space
if (docColorSpace is CMYK) then
set SpotColor1 to {cyan:21.0, magenta:0, yellow:100.0, black:0.0}
else
set SpotColor1 to {red:206.0, green:219.0, blue:41.0}
end if
--color change first
set (path items whose fill color is not SpotColor1)'s fill color to SpotColor1
--get current size
set tempGroup to make new group item with properties {name:"TempGroup"} -- Create a temporary container group
duplicate every page item to beginning of group item "TempGroup" -- copy instances to it
set {w, h} to {width, height} of tempGroup -- get properties
delete tempGroup
display dialog "Width of Art: " & (w / 72) & return & "Height of Art: " & (h / 72)
--change to desired size PROBLEM SECTION
set newWidth to text returned of (display dialog "Please enter Art Width:" default answer "100")
tell application "Adobe Illustrator"
set artWidth to width of tempGroup
set sizeDifference to (newWidth / artWidth) * 100
try
if newWidth is equal to artWidth then
set scalePercentage to 100
end if
if newWidth is greater than artWidth then
set scalePercentage to (100 + sizeDifference)
end if
if newWidth is less than artWidth then
set scalePercentage to (100 - sizeDifference)
end if
end tell
end
I appreciate any suggestions. I realize javascript is probably the way to go with Illustrator
but am trying to saw my way through understanding some applescripts before moving on to something more advanced. Thanks in advance.
I found the answer after playing with the math.
The problem was Illustrator doesn't deal with inches only points
hence the (w/72) (h/72) portion when it gets the size.
72 points = 1 inch so the math had to convert points to inches.
like so
tell application "Adobe Illustrator"
activate
tell document 1
--get current size
set tempGroup to make new group item with properties {name:"TempGroup"} -- Create a temporary container group
duplicate every page item to beginning of group item "TempGroup" -- copy instances to it
set {w, h} to {width, height} of tempGroup -- get properties
delete tempGroup
display dialog "Width of Art: " & (w / 72) & return & "Height of Art: " & (h / 72)
--resize
set newWidth to text returned of (display dialog "Please enter Art Width:" default answer "10")
set artWidth to width of tempGroup
set sum to (artWidth / newWidth * 100)
set scalePercentage to (72 / sum * 10000)
scale tempGroup horizontal scale scalePercentage vertical scale scalePercentage
end tell
end tell
end

AppleScript resize image and save with new name

I'm calling an Applescript from within FilemakerPro.
These are the things I try to achieve:
An image is stored within a container field.
It stores it's name and path in separate fields.
An Applescript is being activated to perform these actions:
It retrieves the data from FilemakerPro
It checks if there is a folder with the same name as the field "calculate_merk_id"
If not, it creates the folder
It creates a new imagefile which name should have "__small" added to his name after it has been resized to 155 x 134, and stores it in this folder
It creates a new imagefile which name should have "__large" added to his name after it has been resized to 400 x 400, and stores it in this folder
The first error I receive is that it cannot retrieve the dimensions of the image. Furthermore, it won't create my resized images ... Anyone who wants to give me a hand in the right direction, please?
set pad to cell "ServerImagePath" of current record
set filenaam to cell "afbeelding_local_vol_pad" of current record
set foldernaam to cell "calculate_merk_id" of current record
set volle_foldernaam to cell "ServerImageFolder" of current record
set volle_filenaam to cell "ServerImageFile" of current record
set target_small_width to 155
set target_small_height to 134
set target_large_width to 400
set target_large_height to 400
tell application "Finder"
if not exists volle_foldernaam then
make new folder at pad with properties {name: foldernaam}
end if
duplicate filenaam to volle_foldernaam with replacing
tell application "Image Events"
launch
set this_image to filenaam
copy dimensions of this_image to {W, H}
if target_small_width is greater than target_small_height then
if W is greater than H then
set the scale_length to (W * target_small_height) / H
set the scale_length to round scale_length rounding as taught in school
else
set the scale_length to target_small_height
end if
else if target_small_height is greater than target_small_width then
if H is greater than W then
set the scale_length to (H * target_small_with) / W
set the scale length to round scale_length rounding as taught in school
else
set the scale_length to target_small_width
end if
else
set the scale_length to target_small_height
end if
scale this_image to size scale_length
pad this_image to dimensions {target_small_width, target_small_height}
save this_image as this_name & "__small"
close this_image
end tell
end tell
You forgot to open the image
set this_image to filenaam
should be
set this_image to open filenaam
Why not do this natively in FileMaker? FM 12 and later have the GetThumbnail() function. A script could calculate a re-sized image into a global field, then export the image from the global field. Completely cross-platform will work on OSX and Windows, even FileMaker Go.
Set Field [ photo_temp_g ; GetThumbnail ( photo_original ; width ; height ) ]
Commit Record/Request []
Export Field Contents [ photo_temp_g ; “<filename>” ]

Why won't Photoshop revert to earlier history state in script?

I've written an Applescript to automate watermarking and resizing images for my company. Everything generally works fine — the script saves the initial history state to a variable, resizes the image, adds the appropriate watermark, saves off a jpeg, then reverts to the initial history state for another resize and watermark loop.
The problem is when I try not to use a watermark and only resize by setting the variable wmColor to "None" or "None for all". It seems that after resizing and saving off a jpeg, Photoshop doesn't like it when I try to revert to the initial history state. This is super annoying, since clearly a resize should count as a history step, and I don't want to rewrite the script to implement multiple open/close operations on the original file. Does anyone know what might be going on? This is the line that's generating the problem (it's in both the doBig and doSmall methods, and throws an error every time I ask it just to do an image resize and change current history state:
set current history state of current document to initialState
and here's the whole script:
property type_list : {"JPEG", "TIFF", "PNGf", "8BPS", "BMPf", "GIFf", "PDF ", "PICT"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png", "psd", "bmp", "gif", "jp2", "pdf", "pict", "pct", "sgi", "tga"}
property typeIDs_list : {"public.jpeg", "public.tiff", "public.png", "com.adobe.photoshop-image", "com.microsoft.bmp", "com.compuserve.gif", "public.jpeg-2000", "com.adobe.pdf", "com.apple.pict", "com.sgi.sgi-image", "com.truevision.tga-image"}
global myFolder
global wmYN
global wmColor
global nameUse
global rootName
global nameCount
property myFolder : ""
-- This droplet processes files dropped onto the applet
on open these_items
-- FILTER THE DRAGGED-ON ITEMS BY CHECKING THEIR PROPERTIES AGAINST THE LISTS ABOVE
set wmColor to null
set nameCount to 0
set nameUse to null
if myFolder is not "" then
set myFolder to choose folder with prompt "Choose where to put your finished images" default location myFolder -- where you're going to store the jpgs
else
set myFolder to choose folder with prompt "Choose where to put your finished images" default location (path to desktop)
end if
repeat with i from 1 to the count of these_items
set totalFiles to count of these_items
set this_item to item i of these_items
set the item_info to info for this_item without size
if folder of the item_info is true then
process_folder(this_item)
else
try
set this_extension to the name extension of item_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of item_info
on error
set this_filetype to ""
end try
try
set this_typeID to the type identifier of item_info
on error
set this_typeID to ""
end try
if (folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
-- THE ITEM IS AN IMAGE FILE AND CAN BE PROCESSED
process_item(this_item)
end if
end if
end repeat
end open
-- this sub-routine processes folders
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
set the item_info to info for this_item without size
if folder of the item_info is true then
process_folder(this_item)
else
try
set this_extension to the name extension of item_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of item_info
on error
set this_filetype to ""
end try
try
set this_typeID to the type identifier of item_info
on error
set this_typeID to ""
end try
if (folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
-- THE ITEM IS AN IMAGE FILE AND CAN BE PROCESSED
process_item(this_item)
end if
end if
end repeat
end process_folder
-- this sub-routine processes files
on process_item(this_item)
set this_image to this_item as text
tell application id "com.adobe.photoshop"
set saveUnits to ruler units of settings
set display dialogs to never
open file this_image
if wmColor is not in {"None for all", "White for all", "Black for all"} then
set wmColor to choose from list {"None", "None for all", "Black", "Black for all", "White", "White for all"} with prompt "What color should the watermark be?" default items "White for all" without multiple selections allowed and empty selection allowed
end if
if wmColor is false then
error number -128
end if
if nameUse is not "Just increment this for all" then
set nameBox to display dialog "What should I call these things?" default answer ("image") with title "Choose the name stem for your images" buttons {"Cancel", "Just increment this for all", "OK"} default button "Just increment this for all"
set nameUse to button returned of nameBox -- this will determine whether or not to increment stem names
set rootName to text returned of nameBox -- this will be the root part of all of your file names
set currentName to rootName
else
set nameCount to nameCount + 1
set currentName to rootName & (nameCount as text)
end if
set thisDocument to current document
set initialState to current history state of thisDocument
set ruler units of settings to pixel units
end tell
DoSmall(thisDocument, currentName, initialState)
DoBig(thisDocument, currentName, initialState)
tell application id "com.adobe.photoshop"
close thisDocument without saving
set ruler units of settings to saveUnits
end tell
end process_item
to DoSmall(thisDocument, currentName, initialState)
tell application id "com.adobe.photoshop"
set initWidth to width of thisDocument
if initWidth < 640 then
resize image thisDocument width 640 resample method bicubic smoother
else if initWidth > 640 then
resize image thisDocument width 640 resample method bicubic sharper
end if
set myHeight to height of thisDocument
set myWidth to width of thisDocument
if wmColor is in {"White", "White for all"} then
set wmFile to (path to resource "water_250_white.png" in bundle path to me) as text
else if wmColor is in {"Black", "Black for all"} then
set wmFile to (path to resource "water_250_black.png" in bundle path to me) as text
end if
if wmColor is not in {"None", "None for all"} then
open file wmFile
set wmDocument to current document
set wmHeight to height of wmDocument
set wmWidth to width of wmDocument
duplicate current layer of wmDocument to thisDocument
close wmDocument without saving
translate current layer of thisDocument delta x (myWidth - wmWidth - 10) delta y (myHeight - wmHeight - 10)
set opacity of current layer of thisDocument to 20
end if
set myPath to (myFolder as text) & (currentName) & "_640"
set myOptions to {class:JPEG save options, embed color profile:false, quality:12}
save thisDocument as JPEG in file myPath with options myOptions appending lowercase extension
set current history state of current document to initialState
end tell
end DoSmall
to DoBig(thisDocument, currentName, initialState)
tell application id "com.adobe.photoshop"
set initWidth to width of thisDocument
if initWidth < 1020 then
resize image thisDocument width 1020 resample method bicubic smoother
else if initWidth > 1020 then
resize image thisDocument width 1020 resample method bicubic sharper
end if
set myHeight to height of thisDocument
set myWidth to width of thisDocument
if wmColor is in {"White", "White for all"} then
set wmFile to (path to resource "water_400_white.png" in bundle path to me) as text
else if wmColor is in {"Black", "Black for all"} then
set wmFile to (path to resource "water_400_black.png" in bundle path to me) as text
end if
if wmColor is not in {"None", "None for all"} then
open file wmFile
set wmDocument to current document
set wmHeight to height of wmDocument
set wmWidth to width of wmDocument
duplicate current layer of wmDocument to thisDocument
close wmDocument without saving
translate current layer of thisDocument delta x (myWidth - wmWidth - 16) delta y (myHeight - wmHeight - 16)
set opacity of current layer of thisDocument to 20
end if
set myPath to (myFolder as text) & (currentName) & "_1020"
set myOptions to {class:JPEG save options, embed color profile:false, quality:12}
save thisDocument as JPEG in file myPath with options myOptions appending lowercase extension
set current history state of current document to initialState
end tell
end DoBig
If you choose a color : save document "Ducky.tif" as JPEG in file ..... the current document will be document "Ducky.tif".
If you choose "None" or "None for all" : save document "Ducky.tif" as JPEG in file ...... the current document will be document "image_640".
So the variable initialState = history state 2 of document "Ducky.tif" give an error, because this document no longer exists.
To leaving original open, here's a solution , use copying true in your save command.
save thisDocument as JPEG in file myPath with options myOptions appending lowercase extension with copying

Resources