I am new to MacScripter and Applescript in general. I’ve been looking for a Applescript solution to a problem I’ve had for a few years now.
I edit and markup text in a content management systems via a web-based interface, not by choice, but by happenstance. To reduce time spent copy-pasting text into an html editor, or manually marking up text, I have a need for an Applescript that would add a specific starting and ending html text tag to a selection of highlighted text to make that text into an element.
For instance, in Safari, I would want to highlight a block of text in a text-input window and activate an Applescript that would encapsulate the highlighted text block in the text markup tag
.
I wrote this script:
tell application "Safari"
activate
set selection to "<blockquote>" & selection & "</blockquote>"
end tell
And it does nothing & obviously telling of my abilities to write an Applescript.
In the end, I want to create a dozen or more Applescript documents for all the most regularly used (by me) text markup tags that I would then hotkey via Key Xing or Keyboard Maestro. In this way, for instance, Cmd+Shift+Option+B would tag a highlighted selection of text with the
Unfortunately your lack of success has more to do with Safari’s lack of AppleScript support than it does your lack of scripting knowledge.
In their infinite wisdom the Safari developers omitted almost any kind of useful AppleScript support, including the ability to get (let alone manipulate) the selection, fill in forms, edit bookmarks, etc., etc., etc.
About the only thing you can do with AppleScript in Safari is open a new browser window and get the text or source of a page. whoopee! :rolleyes:
To do what you want with Safari you’ll have to resort to JavaScript and use Safari’s ‘do JavaScript’ command. Fortunately JavaScript can manipulate the text in a form, but I believe it can only reference form fields by name or ID, meaning it’s not an all-round solution if different pages have different form layouts (maybe not a problem in this case).
Unfortunately my JavaScript knowledge isn’t good enough to whip up a script to do what you want, but I’m sure there’s other JavaScript-savvy people around who can, plus there’s a myriad of web resources on JavaScript that might help.
Both the AppleScript and JavaScript support in Safari is less than satisfying. The Safari JavaScript DOM model doesn’t support “document.selection” nor “document.getSelection” and provides no way to do it in AppleScript either. However, you can cheat by using System Events:
I don’t thoroughly understand what you want, but this may help:
tell application "Safari" to activate
tell application "System Events"
tell application process "Safari" to click menu item "Copy" of menu "Edit" of menu bar 1
set the clipboard to "<b>" & (the clipboard) & "</b>"
tell application process "Safari" to click menu item "Paste" of menu "Edit" of menu bar 1
end tell
You may need to substantially upgrade your OS to use the above. Also, it is beta software. I tested the code above by selecting an few words in a text entry box on a form at a website. It copied the selected text, then pasted it with the markup around it. That is what you want, right?
Here’s what you need to do: find out the name of the form on your web-page. Look at the source code of the page - you can also just use the index of the form, starting at 0 = the first (or only) form is forms[0]. Then, you can use JavaScript to get whatever the current value is, and add your text to it. So, if the form on your page has a text-field whose name (not the label next to it - look in the code) is firstname, you’d do the following to put the blockquote around it:
tell application "Safari"
do JavaScript "
var oldValue=document.forms[0].firstname.value;
document.forms[0].firstname.value='<blockquote>' + oldValue + '</blockquote>';
" in document 1
end tell
That’s all you need. Just change the parts you want differently, and you’re set.
I had the Syntax Error, too, but it disappeared after I upgraded to the latest (1.1.2?) version of System Events.
I double-checked at System:Library:Core Services. I have 1.1.2 (as revealed by the Get Info box) but the file itself claims it’s 1.1.1 (and its dictionary claims its 1.1.1).
The upgrade at apple’s web site (sorry, no url) will probablly fix the matter.
mgh, are you sure about your version numbers? I have System Events 1.1.1 and Get Info says 1.1.1 with a creation date of July 29 2002. I also have System Events and the Get Info for it says 1.2 with a Dec 3 2002. The one that you can download from the AppleScript GUI page has version 1.2 of the System Events. (aka, Dec2002SystemEvents.pkg)
The syntax error disappeared, thanks for the pointer. The Dec2002 SystemEvents installer package, the latest from Apple’s Applescript site, does the trick.
mgh and jonn8’s scripts compile fine now, but they don’t function as I need. Just as a control, I am using google’s form input at google.com with Safari 1.0 (v85). The form’s code, incidentally is:
I type the words, in the google text input window, “this is a test selection” and I highlight the text and activate the applescript from jonn8 and mgh respectively, both via Key Xing and the Applescript menu-widget located in the upper-right side of my monitor. “this is a test selection” remains without start and end html tags. After the Applescript is applied, it should look like:
<blockquote>this is a test selection</blockquote>
Camelot’s suggestion and Krioni’s code is too specific for the various form input fields I encounter, though I appreciate the implimentation because I see a definate use in this code for building a later Applescript.
The following script works for me the first time I compile and run it but then it ceases to work properly in Safari 1.0. On subsequent runs it copies the text but then doesn’t add the tags and update the clipboard and just pastes what was copied essentially leaving it unchanged except that the text itself is no loger selected and the cursor is just right of the selection.
Okay, this is now working reliably for me but it takes an extra 3 seconds. I had a hunch that AppleScript was working faster than the rest of the system so I added some pauses to let everything else catch up and it seems to work every time now.
Jon, it’s working for me as well – great job. I’ve shaved a short time off the action by adjusting the_delay to 0.75 from 1. Too bad about the delay, but it does work!
So, you are entering into an arbitrary form on an arbitrary page? There are no similarities amongst the pages you work with? GUI-scripting is a fortunate last resort, but should be just that - a last resort.
If the forms you are working with are in any way similar (ie. the first text field on each page = form[0].elements[0].value) then you can write a script that does what you want. If you are entering HTML code into the form, isn’t there a possibility that oyu can find similarities and code around them? Look up the document object model (DOM) in JavaScript to get an idea of all the things you can do.
There may even be a “currently selected field” property of a document, although I haven’t found it yet. Internet Explorer has one, called document.activeElement. I’m going to see if there is a way to mimic that capability in Safari. Too bad Apple didn’t support that property themselves.