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
Related
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
I need a simple AppleScript that will cause PowerPoint to move to a specified slide number. I unfortunately am not familiar enough with AppleScript to properly interpret the information in the Applescript Library for PowerPoint. I think if someone can get me started I can take it from there.
The script just needs to reference the active presentation. If I could speak to my computer I just need to tell it to "go to slide #5" or "go to slide #2", etc.
The script needs to work while in Slideshow mode as the presentation will be running on a separate screen. Thanks so much for the help, I haven't been able to find this anywhere.
my gotoSlide(2)
on gotoSlide(whichSlide)
-- whichSlide is the integer position of the slide
tell application "Microsoft PowerPoint"
activate
tell active presentation
set theView to view of document window 1
go to slide theView number whichSlide
end tell
end tell
end gotoSlide
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
I am writing some text in to word file i want to change the color of that text any one can help on that one plz.
I want to print the 'message' from following script in red color.
Here is the Script:
set message to "mostly these windows are popup in application"
on ResultCreationFuction(message)
try
set text_to_save to message as text
tell application "System Events"
tell application "Finder"
set sortedList to sort (get files of folder "SofTestAutomationResult" of desktop) by modification date
set FileCount to get count of sortedList
set theFile to (item FileCount of sortedList) as alias
end tell
set file_ref to open for access theFile with write permission
write (text_to_save & return) to the file_ref starting at eof
close access file_ref
delay 2
end tell
end try
end ResultCreationFuction
Some Details:
The file is word which is all ready present on above location having name "10.012.2014_17_4_20.doc" (the name of .doc file is not fix)
What you are attempting is the wrong way to do it.
To manipulate content like that, including formatted text (not plain
text), you need to work within, ideally, a well-scriptable app, like
Pages (or Word, perhaps, but I don't have that on the machine I'm
writing this from).
Don't use System Events if you don't need to. Use the apps with the appropriate AppleEvents/dictionary, etc. If you don't know what I'm talking about, you need to take advantage of the infinite resource known as the web.
"Fuction" is just bad form.
I would suggest doing a lot more reading up on how AppleScript works (or scripting in general), but to start you out, here is a script I just wrote in pages which sets the color of a specific word of the open document after putting text in there:
tell application "Pages"
set body text of document 1 to "hello there mister fancy pants"
set color of word 3 of body text of page 1 of document 1 to {64614, 0, 111}
end tell
If you have Pages, try this by starting with a blank page and running this script. Obviously, you could get rid of "word 3 of" in the 2nd line, and the whole body text will be red.
I hope this makes sense and is of help.
[edit]
I should mention that even TextEdit is scriptable and can open Word documents. Here's an example using TextEdit:
tell application "TextEdit"
set text of document 1 to "hello mister fancy pants"
set color of words 2 thru 3 of text of document 1 to {65535, 0, 0}
end tell
There is a little danger of non-Word apps losing formatting of Word files. But it just seems you are attempting something very simple, and I'm not sure if Word is really necessary here.
You can't add color using the write to eof. You should open the document in Word and then insert the line and add the color. Here's a script that should demonstrate how:
set text_to_add to "mostly these windows are popup in application"
set theFile to ((path to desktop folder) & "10.012.2014_17_4_20.doc") as string
tell application "Microsoft Word"
set theFile to theFile as string -- assuming theFile is an alias or :: path
open file theFile
tell active document
set endOfDoc to end of content of text object -- insert the text to end of document
set theRange to create range start (endOfDoc - 1) end endOfDoc
insert text text_to_add at theRange
set myRange to create range start endOfDoc end (endOfDoc + (length of text_to_add))
set color index of font object of myRange to red
save
end tell
end tell
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.