• 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: Christopher Stone <email@hidden>
  • Date: Thu, 30 Mar 2017 22:41:52 -0500

On 03/30/2017, at 07:22, Takaaki Naganoya <email@hidden> wrote:
These scripts work well with my macOS 10.12.5beta+Safari 10.1.1.


Hey Takaaki,

Would you test the following scripts for me on the 10.12.5 beta?

They fail in the Safari Technology Preview (10.2) on macOS 10.12.3, and that is very, very bad for me.

Reports suggest the same failure on 10.12.4...

TIA.

--
Best Regards,
Chris

------------------------------------------------------------------------------

# Test this one on a Google search page:

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/03/29 04:44
# dMod: 2017/03/29 04:44 
# Appl: Safari, System Events
# Task: Extract Links from a Google Search.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @System_Events, @Extract, @Links, @URLs, @Google, @Search
------------------------------------------------------------------------------

set xpathStr to "//*[@class=\\'r\\']/a"
set strJS to "

var xpathResults = document.evaluate('" & xpathStr & "', document, null, 0, null),
  nodeList = [],
  oNode;

while (_oNode_ = xpathResults.iterateNext()) {
  nodeList.push(oNode.href);
}

nodeList;

"
# Allow for the possibility that Safari has been hidden.
# When Safari is hidden it does NOT correctly discern window 1.
tell application "System Events"
    tell application process "Safari"
        if its visible is false then set its visible to true
    end tell
end tell

tell application "Safari"
    set linkList to (do _javascript_ strJS in front document)
end tell

------------------------------------------------------------------------------

# Test this one on any page with links:

------------------------------------------------------------------------------

set regexStr to ".*"
set linkList to safari_links(regexStr, "*", "href")

------------------------------------------------------------------------------
--ยป HANDLERS
------------------------------------------------------------------------------
on safari_links(regexStr, tagName, tagType)
    set _javascript_CMD to "function in_array (array, item) {
    for (var i=0; i < array.length; i++) {
        if ( array[i] == item ) {
            return true;}}
    return false;}
    var a_tags = document.getElementsByTagName('" & tagName & "');
    var href_array = new Array();
    var reg = new RegExp(/" & regexStr & "/i);
    for (var i=0; i < a_tags.length; i++) {
        var href = "" style="line-height: normal; font-family: Verdana;" class=""> & tagType & ";
        if ( reg.test(href)) {
            if ( !in_array(href_array, href)) {
                href_array.push(href);}}}

    href_array;
"

    

    try
        tell application "Safari" to set linkList to do _javascript_ _javascript_CMD in document 1
        if linkList = missing value then set linkList to {}
    on error
        set linkList to {}
    end try

    

    return linkList

    

end safari_links
------------------------------------------------------------------------------

 _______________________________________________
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: Takaaki Naganoya <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>)

  • Prev by Date: Re: range of integers?
  • Next by Date: Re: Sierra 10.12.4 and JavaScript by AppleEvents in Safari
  • 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