Apply current selection as a layer mask to the current layer - applescript

I have saved a selection as a channel called "circle".
I want to apply this selection as a layer mask to all layers
This is the script I have so far
tell application "Adobe Photoshop CC 2015.5"
activate
set theDOC to the current document
tell theDOC
set totalLayers to count each layer
repeat with indice from 1 to totalLayers by 1
tell layer indice
-- load the channel
tell theDOC
load selection of it from channel "circle" of it
end tell
-- I now need a magic command to apply the "circle" selection that is active now, to the current layer as a layer mask, inside this loop
end tell
end repeat
end tell
end tell

After a deep research I conclude that there is no way to do that, thanks to Adobe. The solution I have for this is:
create an action on Photoshop to add the selection as a mask.
call that action inside the loop using
do action "mask" from "Default Actions"
change mask and Default Actions with your action name and set where the action is stored.
Enjoy.

Related

Applescript to make the next tab the current document in Photoshop

I'm nearing the end of finishing an application that uses Google Vision to identify articles of clothing, and automatically crop to the article in Photoshop.
The first script I have goes through all of the open images and grabs their dimensions and file paths. Those are then passed to my application to do the rest of the magic.
I ran into an issue where it just kept cropping the same image over and over again. After a bit of debugging I realized that this is due to the fact that it will only crop the image that is "active" in photoshop.
So, I'm faced with the task of needing to call another script that sets the next tab as the active document in PS before hitting the crop function.
I was hoping for something easy like:
tell application "Adobe Photoshop 2020"
set currentDocs to documents
set currentDoc to next item of currentDocs
end tell
Well, that doesn't work...
tell application "Adobe Photoshop 2020"
set currentDocs to documents
-- set myList to {}
repeat with n from 1 to count of currentDocs
set currentDoc to item (n + 1) of currentDocs
end repeat
end tell
This works, but errors after the last tab because it goes out of bounds. (not sure how to stop that?) And the fact that it's in a loop doesn't help either. I just need a simple script that sets the currentDoc to the NEXT open image. Then I Can crop, move to the next image, crop, etc.
But it also needs to know the last tab so it doesn't go back through the images.
Use the repeat with loopVariable (in list) syntax instead.
Then set current document in each turn of the loop.
For example, the following loops over all open documents and crops them to 100 x 100 pixels:
tell application "Adobe Photoshop 2020"
repeat with doc in documents as list
set current document to doc
crop doc bounds {0, 0, 100, 100}
end repeat
end tell

Applescript to make indesign changes to group of text boxes

I currently have an AppleScript that makes small changes to text boxes in an indesign document. The script goes page by page, and if an applied object style is called a certain name and it contains a certain character, it will apply a paragraph style. Here's an example of my script:
tell application "Adobe InDesign"
repeat with x from 1 to count pages
set ThisPage to page x
tell ThisPage
if exists (text frames whose (name of applied object style is "PriceBox" and contents contains "/$")) then
set myFrames to (text frames whose (name of applied object style is "PriceBox" and contents contains "/$"))
tell myFrames
repeat with r from 1 to count items
set applied paragraph style of paragraphs of item r of myFrames to "2forPrice"
end repeat
end tell
end if
end tell
end repeat
end tell
What this does is it goes to the first page of a document, and if there is a text box that is called "PriceBox" and the text box has the characters "/$" in it, it will change the paragraph style to a named style "2forPrice".
This works great when there is 1 "listing" on each page. I am trying to make this work where there are multiple groups of listings and make the script go to each and every group of boxes separately and run the script. Any way to tell if this can be done by group, rather than by page?
Thank you for your help!!
UPDATE:
Here are the screenshots of what it does, I skipped a few steps to make it work, but I rather use groups of boxes.
Picture1: This is what's considered a "group", I want to put all these boxes in one group.
Picture2: Here is how all the groups are laid out on one page
Picture3: Take a look at the changes to the prices if it's a "2for", for example 2/$6
I want my script to go group by group, instead of page by page, to do the if statement and change the paragraph style. Thanks again for your help!
#Ted: Depending on how well an app’s Apple Event Object Model is implemented, you should be able to do all that in a single command:
tell application "Adobe InDesign"
tell front document
tell pages
tell (text frames whose name of applied object style is "PriceBox" and contents contains "/$")
set applied paragraph style of paragraphs to "2forPrice"
end tell
end tell
end tell
end tell
(Tip: Apple event IPC is RPC + queries, not OOP.)
[ETA] As commenter below notes, this won’t work on grouped text frames, so here’s a recursive variation that will:
to walkGroups(groupRef)
tell application "Adobe InDesign CS6"
tell groupRef
tell (text frames whose name of applied object style is "PriceBox" and contents contains "/$")
if it exists then
set applied paragraph style of paragraphs to "2forPrice"
end if
end tell
repeat with subgroupRef in groups
my walkGroups(subgroupRef)
end repeat
end tell
end tell
end walkGroups
tell application "Adobe InDesign CS6"
my walkGroups(front document)
end tell
I think you can use "page items" of the page. If there is a group, it will count as a page item. You will then have to programmatically inspect each group for text frames.
Eg, if there are 3 text frames and one group of 2 text frames, the text frame count for the page will be 3, but the page items count will be 4 (the three ungrouped text frames plus the one group of 2 text frames.
ONE-LINER / STORY approach
As you can see, searching for text frames by scouring pages, spreads, paste boards, groups, etc. can be difficult. The script API doesn't have a command for "give me all text frames in the layout regardless of where they are".
The workaround here is to use the story object. If you ask InDesign to give you a list of all stories (or text flows) in a layout, you'll get them regardless of where they may exist within the layout.
The example below starts with all stories that are associated with a text frame and then matches only the stories where the text frame's object style is "test". It then applies a paragraph style to the paragraphs of those stories.
tell application "Adobe InDesign CC 2018"
set applied paragraph style of paragraphs of (stories of document 1 whose class of (object reference of item 1 of text containers) is text frame and name of applied object style of item 1 of text containers = "test" and contents contains "/$") to "Body Text"
end tell
IF NESTED TELLS ARE PREFERRED
tell application "Adobe InDesign CC 2018"
tell document 1
tell (stories whose class of (object reference of item 1 of text containers) is text frame and name of applied object style of item 1 of text containers = "test" and contents contains "/$")
set applied paragraph style of paragraphs to "Body Text"
end tell
end tell
end tell
I don't really know how to work InDesign — I have an old copy of it, but it would be nice if you provided an example document that I could download and test — but just in terms of AppleScript you probably want to do something like so:
tell application "Adobe InDesign"
repeat with this_page in pages
tell this_page
set text_frames_list to (text frames whose name of applied object style is "PriceBox" and contents contains "/$")
repeat with a_text_frame in text_frames_list
tell a_text_frame
set applied paragraph style of paragraphs of a_text_frame to "2forPrice"
end tell
end repeat
end tell
end repeat
end tell

Applescript w/Keynote. Accessing shapes within a slide

I have a templated Keynote file with some slides and all the slides each have 2 shapes in them. I want to be able to say something like, "hey give me shape 2 of slide 2". The purpose of this, is so that i can add text items directly to that shape. below is the code that I have right now.
I am using the latest Keynote 6.5.2 & Yosemite.
tell application "Keynote"
activate
tell document 1
set anniversary to "Anniversaries"
set myShape to shape 2 of slide 2
tell myShape
set thisTextItem to make new text item with properties {object text:anniversary}
#log thisTextItem
tell thisTextItem
set the size of its object text to 144
set the color of its object text to "blue"
end tell
end tell
end tell
end tell
I can tell slide 2 by itself and of course i get a big text item for slide 2 with text "Anniversaries" and colored blue but its only slide 2... not within shape 2 of slide 2.
With this code it gives me a pop up error when running the script:
Result:
error "Keynote got an error: Can’t make or move that element into that
container." number -10024
what does this mean? do i not have access to shapes within slides?? Any help/info/examples of setting text within a shape that is within a slide would be beneficial. Thanks!
You can set the properties of the text in a shape, you can't insert a text item object in a shape.
tell application "Keynote"
tell document 1
tell shape 2 of slide 2
set object text to "Anniversaries"
tell object text
set it's size to 44
set it's color to {0, 0, 65535} -- blue
end tell
end tell
end tell
end tell

Using AppleScript to set borders in Numbers?

I've successfully managed to get AppleScript to manipulate the background colors in my Numbers document by refering to the background color property. However, I would like to set the borders of a selection or range of cells as well.
I've looked in the Dictionary in AppleScript Editor for a command or property that could help me out here, but I haven't found anything. Is it the case that it's not possible to create borders in Numbers with AppleScript?
There is nothing in the AppleScript dictionary to allow this functionality (which is awful, IMO, but perhaps Apple will add this in the future if enough people complain).
You can use SystemEvents to interact with the interface of Numbers to add the borders:
-- Set the selection range
tell application "Numbers"
make new document
tell front document to tell active sheet to tell table 1
set selection range to range "A3:C6"
end tell
end tell
-- Asks System Events to interact with the interface
tell application "System Events" to tell application process "Numbers"
-- Asks the main window to click on the button called 'Cell' (the name depends of your system language)
tell window 1
click radio button "Cell" of radio group 1
-- Scroll area 4 is the inspector area
tell scroll area 4
-- The first incrementor of the inspector is the borders' one.
increment incrementor 1
end tell
end tell
end tell
If you want to manipulate other elements of the interface, I recommend using the Accessibility Inspector app that comes with Xcode.

PowerPoint Applescript Menu giving different result than AppleScript Editor

The following script, when run in AppleScript Editor returns as text the autoshape type of the objects on the page. However, when run from the applescript menu from within PowerPoint, it returns a script constant instead.
I'm using a more complicated version of this to send properties of the objects to different applications based on what auto shape type it is... tables go one place, placeholders another, and rectangles et al to third. I'm also launching this from within PPT to push out the data, and can't really pull it from any of the other apps, so the AppleScript menu would be where I want it to be.
Can anyone tell me why the same script gives two results?
Thanks,
Alex
tell application "Microsoft PowerPoint"
set currentSlideNumber to slide index of slide range of selection of document window 1
set theSlide to slide currentSlideNumber of active presentation
end tell
getProperty(theSlide)
to getProperty(theSlide)
tell application "Microsoft PowerPoint"
repeat with thisShape in (get every shape of theSlide)
set shapeType to shape type of thisShape
set shapeContent to content of text range of text frame of thisShape
display alert (shapeType as string)
end repeat
end tell
end getProperty
Converting constants (or application properties which standard applescript doesn't understand) to text is tricky. My guess is that when in the powerpoint menu the script understands the constant better and thus you see that.
In any case, I had the same issue with Finder constants and decided to handle the conversion to text myself with an if statement. It's a cumbersome solution because you have to account for every constant but at least you know it will happen properly.
Here's an example. Suppose there is a shape constant of "rect" which stands for a rectangle, and "circ" standing for circle.
NOTE: you can probably use the name of the actual property instead of the constant in the code.
to getProperty(theSlide)
tell application "Microsoft PowerPoint"
repeat with thisShape in (get every shape of theSlide)
set shapeType to shape type of thisShape
if shapeType is <<rect>> then
set shapeTypeText to "rectangle"
else if shapeType is <<circ>> then
set shapeTypeText to "circle"
end if
set shapeContent to content of text range of text frame of thisShape
display alert (shapeTypeText)
end repeat
end tell
end getProperty

Resources