Applescript Illustrator scale art to returned dialog - applescript

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

Related

Can I read/write the name of the Illustrator artboard in Applescript

Is it possible to get the name of an Illustrator artboard in Applescript?
This script works perfectly until I try to get the name of the artboard:
tell application "Adobe Illustrator"
tell document 1
set artboards_count to count of artboards
set c to 1
repeat while c <= artboards_count
log index of artboard c as text
log artboard rectangle of artboard c as text
log name of artboard c as text -- this line fails
set c to c + 1
end repeat
end tell
end tell
the line log name of artboard c as text fails - everything else works ok.
The message is:
Adobe Illustrator got an error: Can’t get name of artboard 1 of document 1. (-1728)
Any idea as to why?
Setting the name fails too, BTW. However if I do
tell application "Adobe Illustrator"
tell document 1
return properties of artboard 1
end tell
end tell
I get (carriage returns added for clarify):
artboard rectangle:0.0, 0.0, 841.889999999999, -595.280000000001,
ruler PAR:1.0, show center:false, show cross hairs:false,
show safe areas:false, ruler origin:0.0, 0.0, name:Artboard 1,
container:document 1, best type:reference, default type:reference,
class:artboard, index:1
from which one would think the property nameshould be there.
The name property is read-only so there is no way to change it once the artboard exists. And even though you can get the properties of an artboard, you can’t convert it to a string if you simply want to target a particular artboard. But there is a way to do it nonetheless that I discovered by accident. Suppose you have several artboards and want to target the artboard named "Squash this". Here's how to do that:
Tell application "Adobe Illustrator"
tell current document
set artCount to number of artboards
repeat with i from 1 to artCount
set artProp to get properties of artboard i
try
set propString to artProp as string --this will fail
on error
set errorDisp to text of result --this captures the text of the error
set errorDispText to errorDisp as string --changes the text to a ¬
searchable string
end try
if errorDispText contains "quash" then
display notification "errorDispText" --oddly enough, this displays just ¬
the artboard name
exit repeat
end if
end repeat
end tell
end tell
The best solution could be coercion; take the record returned from the Artboard properties and coerce the record into a list. So, your name property from the record becomes the 7th item in the list. The code below will get Artboard dimensions, name and index, and place them into a list for later use in your script.
Hope this helps!
tell application "Adobe Illustrator"
set artboardDetails to {}
tell front document
set allArtboards to every artboard
repeat with i from 1 to count of allArtboards
set thisArtboard to item i of allArtboards
set thisArtboardProps to properties of thisArtboard
set thisArtboardDimensions to artboard rectangle of thisArtboardProps
set thisArtboardIndex to index of thisArtboardProps
--WORKAROUND
set thisArtboardPropsCoerce to thisArtboardProps as list
set thisArtboardName to item 7 of thisArtboardPropsCoerce
set the end of artboardDetails to {thisArtboardName, thisArtboardIndex, thisArtboardDimensions}
end repeat
end tell
end tell
(*
--WORKAROUND
The following is a workaround for error returned from:
set anArtboardName to the name of anArtboard -- this line will not return a result, just errors out
BUT WE NEED TO WATCH OUT FOR THE COERCED LIST!
So, we have to coerce the properties record of artboard to a list first
WEIRD I KNOW!
The coercion then changes the record of 11 items to a list of 12 items
set anArtboardProps to properties of anArtboard as list
set anArtboardName to item 7 of anArtboardProps
NOW LETS MATCH UP THE COERCION ITEMS
The first lines are from the record properties, and the second lines are from the coerced to list versions.
01. artboard rectangle:{0.0, 768.0, 1366.0, 0.0},
01. {0.0, 768.0, 1366.0, 0.0},
02. ruler PAR:1.0,
02. 1.0,
03. show center:false,
03. false,
04. show cross hairs:false,
04. false,
05. show safe areas:false,
05. false,
06. ruler origin:{0.0, 0.0},
06. {0.0, 0.0},
07. name:"Artboard 1",
07. "Artboard 1",
08. container:document 1,
08. document 1,
09. best type:reference,
09. reference,
10. default type:reference,
10. reference,
11. index:1
11. artboard,
12. NULL - nothing in the record
12. 1 - so this line is the index of the artboard
*)
Here is my workaround. As the «class bAl9» vanishes every time you compile, you have to copy/paste every time, but it works. And yes, «class bAl9» will turn to "name" and will not compile correctly the next time. Thanks Adobe !
tell application "Adobe Illustrator"
tell document 1
repeat with x in (every artboard)
log index of x as text
log artboard rectangle of x as text
log «class bAl9» of x as text -- artboard's name property = «class bAl9»
end repeat
end tell
end tell
The name property of artboards is read/write, it's easy to rename artboards this way once you get the trick.
Edit : For the loop in a list, I always use the repeat with x in l statement. It's smart and fast.
Stu's answer was the best that I found. Coercing the record to a list is brilliant – I didn't even know you could do that. My code was:
repeat with i from 1 to the (count of AllArtBoards)
set ThisArtBoard to item i of AllArtBoards
set ThisArtBoardName to item 7 of ((properties of ThisArtBoard) as list)
set the end of AllArtBoardNames to ThisArtBoardName
end repeat

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

applescript Illustrator scaling based on return dialog

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

Resources