• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari


  • Subject: Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari
  • From: Shane Stanley <email@hidden>
  • Date: Fri, 31 Mar 2017 18:23:28 +1100

On 31 Mar 2017, at 3:40 pm, Christopher Stone <email@hidden> wrote:

I have workflows that have depended upon driving _javascript_ with AEs for over a decade.

Maybe it's time for a change...

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theQuery to "AppleScriptObjC"
-- build and escape query string
set theQuery to current application's NSString's stringWithString:theQuery
set theQuery to theQuery's stringByReplacingOccurrencesOfString:space withString:"+"
set escQuery to theQuery's stringByAddingPercentEncodingWithAllowedCharacters:(current application's NSCharacterSet's URLQueryAllowedCharacterSet())
-- do search
set searchURLString to current application's NSString's stringWithFormat_("https://www.google.com/search?client=safari&rls=en&ie=UTF-8&oe=UTF-8&q=%@", escQuery)
set theURL to current application's NSURL's URLWithString:searchURLString
set {theData, theError} to current application's NSData's dataWithContentsOfURL:theURL options:0 |error|:(reference)
if theData = missing value then error (theError's localizedDescription() as text)
-- convert to XML
set {theXMLDoc, theError} to current application's NSXMLDocument's alloc()'s initWithData:theData options:(current application's NSXMLDocumentTidyHTML) |error|:(reference)
if theXMLDoc = missing value then error (theError's localizedDescription() as text)
-- filter via XPath and predicate
set xpathStr to "//*[@class=\"r\"]/a/@href"
set {theNodes, theError} to theXMLDoc's nodesForXPath:xpathStr |error|:(reference)
if theNodes = missing value then error (theError's localizedDescription() as text)
set nodeStrings to (theNodes's valueForKey:"stringValue")
set thePred to current application's NSPredicate's predicateWithFormat:"self BEGINSWITH '/url?q='"
set nodeStrings to nodeStrings's filteredArrayUsingPredicate:thePred
-- trim URL strings
set theURLStrings to {}
repeat with aString in nodeStrings
set theOffset to (aString's rangeOfString:"&")
set end of theURLStrings to (aString's substringWithRange:{7, (theOffset's location) - 7}) as text
end repeat
return theURLStrings
(*
--> {
"http://macosxautomation.com/applescript/apps/", 
"https://www.macosxautomation.com/applescript/apps/book.html", 
"https://developer.apple.com/library/content/releasenotes/ScriptingAutomation/RN-AppleScriptObjC/", 
"http://www.peachpit.com/articles/article.aspx?p=1942301", 
"http://macscripter.net/viewtopic.php?id=30274", 
"https://www.jamf.com/jamf-nation/discussions/10600/fun-with-applescriptobjc", 
"http://latenightsw.com/applescriptobjc-in-script-debugger-6/", 
"http://www.mugginsoft.com/kosmictask/help/languages/applescript-cocoa", 
"https://www.youtube.com/watch?v=HE5jFCfkD0w", 
"https://www.safaribooksonline.com/library/view/learn-applescript-the/9781430223610/ch30.html"
}
*)


-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari
      • From: Christopher Stone <email@hidden>
References: 
 >Sierra 10.12.4 and JavaScript by AppleEvents in Safari (From: Christopher Stone <email@hidden>)
 >Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari (From: Takaaki Naganoya <email@hidden>)
 >Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari (From: Christopher Stone <email@hidden>)
 >Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari (From: Takaaki Naganoya <email@hidden>)
 >Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari (From: Christopher Stone <email@hidden>)
 >Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari (From: Takaaki Naganoya <email@hidden>)
 >Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari (From: Christopher Stone <email@hidden>)

  • Prev by Date: Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari
  • Next by Date: Re: 10.12.4 ?
  • Previous by thread: Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari
  • Next by thread: Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari
  • Index(es):
    • Date
    • Thread