I have an AppleScript that I wrote for Numbers a few years ago that used to work. Since then, I've upgraded my OS (probably more than once), which I'm sure also upgraded Numbers and other aspects of the system. My current environment is:
MacOS 12.2.1 (Monterey)
Numbers 11.2
The script appears to be failing (i.e., not doing what I want) when trying to iterate through the range of pre-selected cells:
...
tell application "Numbers"
set activeSheet to active sheet of front document
set scriptStart to current date
tell table 1 of activeSheet
set selRng to cells of selection range
set cellCount to count of selRng
repeat with currentCell in cells of the selRng
my processCell(currentCell)
end repeat
end tell
set scriptEnd to current date
beep
display dialog "DONE: " & my formatTime()
end tell
...
I know from debugging statements (not shown) in the loop that it never gets there and so repeat with currentCell in cells of the selRng is not the correct way to loop through the selRng (which I've also verified contains eight cells).
I'm going to continue trying out different things by searching the web for (good) examples, but if someone here can provide pointers it might save me a lot of time...
Like I said, this used to work...
UPDATE-1: As with my previous posting, I've seemed to solve the initial question, but then the problem just morphs into something else. The loop changed to:
repeat with currentCell in selRng
my processCell(currentCell)
end repeat
OR:
repeat with currentCell in selRng
set cellName to name of currentCell
my processCell(cellName)
end repeat
In the first case, I end up with currentCell being set to:
item 1 of {cell "D122" of table 1 of sheet 2 of document id "66DF865E-D1EC-4E4A-8A0E-E66213CF0E76", cell "E122" of table 1 of sheet 2 of document...}
Which doesn't work because it isn't a cell, it still looks like a range.
In the second case, I end up with a string "D122" which doesn't work for the called routine that expects an actual cell to be passed to it.
So now I either have to figure out how to just get the actual first cell in the range (first coding) or how to convert the string to an actual cell or cell reference (second coding)...
UPDATE-2:
If I change the repeat loop to:
repeat with i from 1 to count of selRng
set currentCell to cell i
my processCell(currentCell)
end repeat
currentCell is set to A1 - not what I want.
If I change it to:
repeat with i from 1 to count of selRng
set currentCell to cell i of selRng
my processCell(currentCell)
end repeat
I get an error "Can’t get cell 1 of {cell "K122" of table 1 of sheet 2 of document id "66DF865E-D1EC-4E4A-8A0E-E66213CF0E76" of application "Numbers"}."
So the first case gives me the wrong cell, and the second case appears to leave me where I was before, trying to extract the i'th cell element of the range. (Arrgh :-))
Well, I figured it out. The new code is:
tell application "Numbers"
set activeSheet to active sheet of front document
set scriptStart to current date
tell table 1 of activeSheet
set selRng to cells of selection range
repeat with i from 1 to count of selRng
set currentCell to item i of selRng
my processCell(currentCell)
end repeat
end tell
set scriptEnd to current date
beep
display dialog "DONE: " & my formatTime()
end tell
Note the use of item I of selRng instead of cell I of selRng
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
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>” ]
I have made a simple script application which accepts radius as input from user and calculates and displays area of circle based on input:
-- initializing radius variable to some text
set radius to "Some text"
repeat until class of radius is number
-- asking user for radius
display dialog "Enter radius: " default answer "" buttons {"Done"} default button 1
set userInput to text returned of result
-- try to check if user enters radius as number
try
-- converting input from user to number
set radius to userInput as number
-- if input is found as number then below code is executed
-- obtaining radius from handler
set circleArea to calculateCircleArea(radius)
-- displaying radius and area of circle obtained to user
display dialog "Circle area for radius: " & radius & " is: " & circleArea buttons {"OK"} default button 1
end try
end repeat
-- handler definition
on calculateCircleArea(parameterRadius)
set areaOfCircle to pi * (parameterRadius ^ 2)
end calculateCircleArea
When I executed the above script and entered some text for the first time, it again asked me to enter the radius, this time I entered some number and it displayed the area of circle but it again started asking for radius as input from user.
Can anyone suggest me where I am wrong in the above script?
Thanks,
Miraaj
repeat until class of radius is integer or class of radius is real
I'm trying a access the complete reference for a cell in Applescript. So far I've managed to get the cell reference and the table reference using a script like:
tell application "Numbers"
tell document 1
repeat with i from 1 to count of sheets
tell sheet i
repeat with j from 1 to count of tables
tell table j
try
set currentCell to the first cell of the selection range
return name of currentCell
end try
end tell
end repeat
end tell
end repeat
end tell
end tell
I can't seem to get the same structure to work for getting the sheet or the document reference. I have tried accessing the properties of the cell and I get something like:
{column:column "A" of table "Table 1" of sheet "Sheet 1" of document "Untitled" of
application "Numbers", alignment:center, value:0.0, background color:{59111, 59111,
59111}, text color:{0, 0, 0}, font size:10.0, vertical alignment:top, name:"A1",
class:cell, font name:"HelveticaNeue", format:automatic, row:row "1" of table "Table
1" of sheet "Sheet 1" of document "Untitled" of application "Numbers", text
wrap:true}
The column property of a cell therefore seems to include the complete reference but if I access the reference directly through the column property.
Can anyone give me some guidance as to how I get the sheet and document using applescript.
Cheers
Ian
Found the solution, fairly simple as long as the code is in the right order:
tell application "Numbers"
tell document 1
repeat with i from 1 to count of sheets
tell sheet i
repeat with j from 1 to count of tables
try
tell table j
set currentCell to the first cell of the selection range
set therow to row of currentCell
set sheetNumber to i
end tell
end try
end repeat
end tell
end repeat
return name of sheet sheetNumber
end tell
end tell
Can use similar code to check for the document number