Using AppleScript to hide Keynote Text Fields in A Slide - macos

I am no AppleScript Jedi, I've only done a few simple things, but I haven't been able to figure this one out and could use some help:
My wife uses slides for her Art History courses and would like to use the same slides for exams (sans identifying names). Rather than create a new presentation, I'd like a tool that iterates through the slides and hides the text fields.
Looking through the Keynote dictionary didn't give me any clues as to how to approach this, any ideas?

AFAIK, with Applescript you can only access the title and the body boxes of the slides. If the text you wish to remove is consistently in either of these boxes the simplest solution would be to loop through the slides replacing that text and then saving a copy of the document.
tell application "Keynote"
open "/Path/To/Document"
repeat with currentSlide in slides of first slideshow
set title of currentSlide to " "
set body of currentSlide to " "
end repeat
save first slideshow in "/Path/To/Document without answers"
end tell
If the text is in a container created with the textbox tool, I don't think you can solve it with Applescript, but Keynote uses an XML based file format, so you could try doing it by editing the XML with your scripting language of choice. The XML schema is documented in the iWork Programming Guide.

Related

Opening Adobe Indesign on other user's computer puts objects in groups

We use ExtendScript with Adobe Indesign. Part of the workflow is to use a InDesign document as a template. The ExtendScript open a InDesign document, and replaces some text and images, and saves a pdf.
e.g. part of the code.
app.activeDocument.textFrames.item('design.name').contents= "John Doe"
Most items in the document are named. (done by modifying the names of the element in the Layer Toolbar)
Now when another user opens this document and saves it, it will place the elements in a group automatically.
This causes the script to stop working because the reference of textFrames.item('design.name') isn't available anymore.
How to handle this issue? because I don't want to manually move the elements out of the group after our designer has changed the InDesign document. Any suggestions are welcome.

Support "styled text" in a scriptable Mac application (Cocoa Scripting)

My app supports being scripted with Applescript.
I am trying to make styled text content, stored in NSAttributedString objects, available to an Applescript user.
I thought I could simply deliver styled text with the NSAttributedString class, just like I deliver plain text with the NSString class, but that does not work - Cocoa Scripting then reports that it cannot convert or coerce the data.
I wonder if I'm missing something or if this is just plain impossible with the standard classes supported by Cocoa Scripting?
AppleScript does know the "styled text" type, as seen in this example:
set stxt to "foo" as styled text
So, if AppleScript knows this type by default, shouldn't the Cocoa Scripting engine support it as well somehow?
As always there are many choices for solving an AS problem.
In my scriptable text editor (Ted), I implemented the Text Suite, which is based on rich text (NSTextStorage, a subclass of NSMutableAttributedString). I wanted to be able to script tabs in my paragraphs, so I added a style record, which contains all the paragraph style information. This lets me write scripts like this:
tell application "Ted"
set doc1 to make new document at beginning with properties {name:"Document One"}
tell doc1
set p1 to make new paragraph at end with data "Paragraph One" with properties {size:24, color:maraschino}
set p2 to make new paragraph at end with data "Paragraph Two" with properties {style:style of paragraph 1}
set color of paragraph 1 to blue
end tell
set doc2 to make new document at beginning with properties {name:"Document Two"}
copy p1 to beginning of doc2
properties of paragraph 1 of doc2
end tell
Since p1 is rich text, the second document ends up with both the text and formatting of the first paragraph of the first document.
You can also ask for the properties of a piece of text, where I have implemented the usual Text Suite properties, as well as a "style" property for paragraph style (backed by NSParagraphStyle, since I wanted to be able to script the tab stops):
properties of paragraph 1 of doc2
Result:
{height:60.0, italic:false, size:24, style:{paragraph spacing after:0.0, head indent:0.0, line break mode:0, alignment:4, line spacing:0.0, minimum line height:0.0, first line head indent:0.0, paragraph spacing before:0.0, tabs:{"28L", "56L", "84L", "112L", "140L", "168L", "196L", "224L", "252L", "280L", "308L", "336L"}, tail indent:0.0, maximum line height:0.0, line height multiple:0.0, default tab interval:0.0}, color:blue, width:164.109375, font:"Helvetica", bold:false, class:attribute run}
This works well for passing rich text within my application, but may not be as useful for passing styled text to other applications, which may be what you wanted to do. I think adding a "style" property (of type record) is probably the best way to convey style info for use in other scriptable apps. Then in the second app, the scripter can make use of any properties in the style record that the second app understands.
It looks like there is no implicit support for styled text in AppleScript. And there is also no common interchange record type for passing styled text.
AppleScript was developed in the pre-OSX days when styled text was often represented by a combination of a plain text (in System or MacRoman encoding) and a styl resource. With Unicode came an alternative format of a ustl style format. These are still used with the Carbon Pasteboard API (PasteboardCreate etc.) today. Yet, none of these seem to have made it into the use with AppleScript.
The fact that AppleScript knows of a styled text type has no special meaning. Even its class is just text.
Update
I just found that Matt Neuburg's book "AppleScript The Definitive Guide" mentions styled text and gives an example where it's indeed showing a record containing both the plain text (class ktxt) and style data (class ksty) with data of type styl, just as I had expected above. He also points out that most applications don't use that format, though.
So, it appears using a record with style resource data is indeed the intended way, only that hardly anyone knows about it. Go figure.

Extract Hyperlinks From Rich Text Clipboard Contents Or Text Selection On The Mac

I would like to be able to get a list of all the hyperlinked URLs in any formatted text that I select on the Mac (formatted text such as a web page or a word processor document).
Preferably I'd like use Applescript or Automator to extract this list of hyperlinks from the text (so that I can then use Applescript to perform further processing on these URLs).
Note that I am talking about hyperlinks being extracted from formatted text, not just extracting URLs from text containing plaintext URLs.
This hyperlink extraction from formatted text seems like it should be a simple programming task, but I have been struggling to find a way to do this in either Applescript or Automator.
Automator can be set to accept rich text input from a text selection, or can input rich text from the clipboard, but I cannot find any way to access this rich text as a string within Automator or Applescript, such that I can then extract the hyperlinked URLs from the string of rich text data.
Once I get access to the rich text data as a string, there will be no problem in extracting the URLs.
Any suggestions on how I might solve this issue are gratefully received.
Applescript itself does not unpack embedded text, so you'll have to use a helper app one way or another. You can use do shell script 'textutil' to do some unimbedding of links:
perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' |
textutil -stdin -stdout -convert html -format rtf
Then, you'll have to extract the URLs. I would suggest using the Automator action 'Extract Data' to do this. If you set the whole thing up as an Automator Workflow, you could invoke it from Applescript. Or, if you save it as a Service, you can just run the whole thing from the Service.
Here's a screenshot of that method that should show what you need:
Let me know if you have questions. You can see variations on this technique here.
Update:
If you want to create this into a Service, it is difficult to coerce the built-in input from Automator into RTF. An effective method is to ignore the input and do a
keystroke "c" using command down
to copy the selected text to the clipboard and then use the workflow from there. See example:

Dynamically change text in Keynote slide using Applescript

What i am wondering is, is there a way to load up a keynote slide, have the slide run an applescript(which reads a text file) and takes that information and uses it to populate text fields on that keynote slide?
Example: Open Keynote presentation. Play the presentation. When we get to slide #4, launch the applescript to get the info that you need from the text file, then populate the text field.
It's an old question but the answer is yes. You can use Applescript to run through the presentation and pause on whatever slide you like. The boxes for inserting text are called "text items" and you can set their contents through the object text property. I found this site very helpful (link goes to the text item page)

Copy Selected Text with VBA and the Adpbe PDF Reader Control

I am trying to copy selected text directly out of a PDF viewer I have made with the Adobe PDF Reader Control in Visual Basic 2010.
I can utilize highlight text with a macro in Word using something this:
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
I am having trouble figuring out whether this bit of code can be used to perform actions with the highlighted text with Adobe PDF Reader. If not, does anyone know how I would go about doing that?
I don't think that the Adobe PDF viewer supports VBA.
However, I would suggest an indirect approach, using the clipboard.
By sending a control+c key, the content of the selection will be transferred to the clipboard, and the clipboard could be processed using "native" VBA.
Sending a control+c is done by using SendKeys("^C"), see This link
And the Clipboard can be manipulated using This exampe

Resources