|
|
Correction and Additions
I think you're missing something in the section about getting info about the tabs in the front window. The line:
doesn't compile properly, and certainly doesn't work, unless you have some strange AppleScript plugin installed. To get information about the open tabs of the front window, you can use the following:
Note also that this returns the result, rather than just logging it in the event listing.
Another very useful AppleScript command for Safari is 'do Javascript' - it lets you run Javascript code in Safari for a specific document, allowing you to do many things the Safari doesn't make directly available via AppleScript. For example, you can get the selected text (as noted in some other hints on this site) using the Javascript code: "document.getSelection();" You can also run Javascript code that gets all the links from a page by looping over the array documents.links. Or, get all the image URLs by looping over document.images. I think you get the idea. Almost anything you can do via Javascript is possible. You can fill out and submit web forms, modify CSS properties of DIVs to hide them, change colors, and much more. I had example code, but something strange is making any attempt to copy my code from the MacOSXHints.com Preview and paste into AppleScript Editor result in failure, even though the code is identical to what ran successfully prior to posting. I ran out of time to figure out what is going on - has anyone else ran into the situation where perfectly good code pasted into a comment here, then previewed, then copied, pasted back into Applescript Editor, compiled without fail - fails to run properly? Then, if I make an equivalent-result change to the Javascript code in the string in AppleScript (like adding a single space after the semi-colon in the block of JavaScript code), it runs properly. Then, if I repeat the posting-preview copy attempt, THAT code fails in AppleScript Editor, and removing the "extra space" now allows it to run successfully. It seems that Safari ignores code that ran successfully before. Is there perhaps some strange issue with encoding going on here? Perhaps line returns copying strangely from Safari? Example: The following ran correctly in AS Editor:
Then, I previewed it in square-bracket code tags here on MacOSXHints.com. I copied the resulting code, pasted into AS Editor, and got an error that "selectedText is not defined" - in other words, the JS code failed to run. So, I added a space AFTER the semi-colon inside the double-quotes for jsCode so that line looked like this:
Now it ran properly. So, I took THAT, pasted into square-bracket code tags here, previewed, copied the preview, pasted into AS Editor, and now THAT failed. Removing the extraneous space made it function again.
Basically, it seems like something strange and invisible is added to the string defining the jsCode variable, but modifying the string in AS Editor and recompiling somehow strips back out that problem. Has anyone run across this before? It's making me crazy - I was trying to post some useful code here, stuff that I use all the time, but something made it impossible to copy the result, even though everything looks perfect. ---
http://www.danshockley.com
Correction and Additions
with respect to copying code from web pages: You have to be a bit careful. Web pages render special characters for a lot of whitespace, and simply copying them straight into an editor that supports rich text (such as the applescript editor) will sometimes produce unexpected results. It shouldn't happen within a code block, but... If you have trouble, copy the code first into a plain text editor (like TextWrangler) and that should normalize all the whitespace. |
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysNo new commentsLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.05 seconds |
|