Friday, August 14, 2020

#1 2020-01-15 08:52:30 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 333

My first AppleScriptObjC

Hi.

Gist at GitHub have very useful code that people share,  more and 3000 result on only AppleScript.

So I made my first AppleScriptObjC script.

It use javascript to push the button 'Raw' to display raw code in safari.
Save the file to desktop with the basename of the script.

I will be more and happy if anyone like to comment, suggest or improve. THANKS. smile)

TODO: Handle any characters include spaces in the filenames.

NOTE: The code is limited in a way It only take the first raw button, for element 8.

I'm thinking maybe a function in javascript that count element index > 8 and build array or something. So the script could get everything...

URL: https://gist.github.com/search?utf8=✓&q=applescript

Applescript:

(*
   My first AppleScriptObjC
       Download content from URL GitHub Gist at 'gist.github.com'
       Use javascript to push 'raw' button, next save the displayed text to desktop.
*)


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

to clickClassName(theClassName, elementnum)
   tell application "Safari"
       activate
       do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
   end tell
end clickClassName

clickClassName("btn btn-sm", 8) -- Click Raw button

delay 8

tell application "Safari"
   set thisURL to document of front window
   set theName to name of thisURL
   set theURL to current application's NSURL's URLWithString:theName
   set nameString to theURL's lastPathComponent as text
   set sourceURL to source of thisURL
   display notification nameString with title "Added Download"
end tell

set theFile to (POSIX path of (path to desktop)) & nameString
set theFileHandle to open for access theFile with write permission
write sourceURL to theFileHandle
close access theFileHandle

Last edited by Fredrik71 (2020-01-15 09:45:25 am)


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.


Filed under: AppleScriptObjC

Offline

 

#2 2020-01-15 10:32:25 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4568

Re: My first AppleScriptObjC

I didn't tested the instruction supposed to click a button.
I had to make numerous changes.

Applescript:


(*
My first AppleScriptObjC
Download content from URL GitHub Gist at 'gist.github.com'
Use javascript to push 'raw' button, next save the displayed text to desktop.
*)


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

to clickClassName(theClassName, elementnum)
   tell application "Safari"
       activate
       do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
   end tell
end clickClassName

--clickClassName("btn btn-sm", 8) -- Click Raw button

--delay 8

tell application "Safari"
   set thisDocument to document of front window
   --> document "Script 33.txt · GitHub"
   set theName to name of thisDocument
   --> "Script 33.txt · GitHub"
   set thisURL to URL of thisDocument
   --> "https://gist.github.com/OKsign/2dd5808d8359aa7026b2d6d9b4d085fc"
   set theURL to current application's NSURL's URLWithString:theName --> missing value
   set theURL to current application's NSURL's URLWithString:thisURL
   get (theURL's |path|()) as text --> "/OKsign/2dd5808d8359aa7026b2d6d9b4d085fc"
   set nameString to (theURL's lastPathComponent()) as text --> "2dd5808d8359aa7026b2d6d9b4d085fc"
   set sourceURL to source of thisDocument
   (*
   --> "






<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<link rel=\"dns-prefetch\" href=\"https://github.githubassets.com\">
<link rel=\"dns-prefetch\" href=\"https://avatars0.githubusercontent.com\">
<link rel=\"dns-prefetch\" href=\"https://avatars1.githubusercontent.com\">
<link rel=\"dns-prefetch\" href=\"https://avatars2.githubusercontent.com\">
<link rel=\"dns-prefetch\" href=\"https://avatars3.githubusercontent.com\">
<link rel=\"dns-prefetch\" href=\"https://github-cloud.s3.amazonaws.com\">
<link rel=\"dns-prefetch\" href=\"https://user-images.githubusercontent.com/\">
"
*)

end tell
-- better to do that out of the tell application block
display notification nameString with title "Added Download"

set nameString to my remplace(nameString, {space, quote, "/"}, "_")
--> "2dd5808d8359aa7026b2d6d9b4d085fc" -- here they where nothing to change
set theFile to ((path to desktop as text) & nameString & ".txt") as «class furl»
set theFileHandle to open for access theFile with write permission
write sourceURL as «class utf8» to theFileHandle
close access theFileHandle



#=====
(*
replaces every occurences of d1 by d2 in the text t
*)

on remplace(t, d1, d2)
   local oTIDs, l
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
   set l to text items of t
   set AppleScript's text item delimiters to d2
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end remplace

#=====

You may edit the list of characters you want to edit.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 15 janvier 2020  17:32:07

Offline

 

#3 2020-01-15 11:34:44 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 333

Re: My first AppleScriptObjC

Hi.

Thanks for your feedback, I try your code and it works.

My idea was to automate Safari to push elements in the URL, there I do not not need to click
manually. I find out later that some links could have more and 1 elements.

ex.

If a user at Gist make 3 files that belong together. In other words it will be 3 documents.

The first document in that link will be elements 8 because Gist GitHub use 7 elements at top
of the page. Element index of the other 3 documents will be (9,10,11) of the element index.

do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1

CLASS:btn btn-sm
Elementnumber: 8 (the first title 'Raw' button of any document at gist GitHub)

If I setup array of of urls of interest it would be possible to automate everything with few clicks.

So what I'm looking for is a way to Automate Safari to get the information to file. And that why
I use javascript it's also a little research and get better knowledge in javascript to use with Safari.

That was my idea... maybe not the best one or fastest.. but learning a lot.. smile)

That said...

If you like to try...
How do I automate Safari to download 4 documents of this url...
https://gist.github.com/kaustavha/25388d53d39b55af511d

It means the only thing I should do is give Safari the above link and start Automation. smile)

Your way is fast, so thanks.


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.

Offline

 

#4 2020-01-15 01:37:33 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 333

Re: My first AppleScriptObjC

URL: https://gist.github.com/kaustavha/25388d53d39b55af511d

I made a quick demo what I want...

Set Safari to location URL above and run my script.

It will download 4 document to the desktop.

Note:
I try to use history.back() but it didn't work, so I use a custom shortcut cmd + arrow left key
My computer is not so fast so 'delay of 5' not all people have hundreds of tabs like me. wink...

But you get a better understanding what I trying to do.


Applescript:

(*
   My first AppleScriptObjC
       Download content from URL GitHub Gist at 'gist.github.com'
       Use javascript to push 'raw' button, next save the displayed text to desktop.
*)


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

activate application "Safari"

getData(8)
getData(9)
getData(10)
getData(11)

on getData(_elementIndex)
   my clickClassName("btn btn-sm", _elementIndex) -- Click Raw button
   delay 5
   my getDocumentSafari()
   delay 5
   my backHistory()
   delay 5
end getData

to clickClassName(theClassName, elementnum)
   tell application "Safari"
       if frontmost is true then
           activate
           return do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
       end if
   end tell
end clickClassName

on backHistory()
   activate application "Safari"
   tell application "System Events"
       key code 123 using command down
       -- return do JavaScript "history.back(1)" in document 1
   end tell
end backHistory

on getDocumentSafari()
   tell application "Safari"
       if frontmost is true then
           set thisURL to document of front window
           set theName to name of thisURL
           set theURL to current application's NSURL's URLWithString:theName
           set nameString to theURL's lastPathComponent as text
           set sourceURL to source of thisURL
           display notification nameString with title "Added Download"
       end if
   end tell
   
   set theFile to (POSIX path of (path to desktop)) & nameString
   set theFileHandle to open for access theFile with write permission
   write sourceURL to theFileHandle
   close access theFileHandle
end getDocumentSafari


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.

Offline

 

#5 2020-01-15 02:56:44 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4568

Re: My first AppleScriptObjC

I don't know javaScript so I rely upon AppleScript.
Here, if the web page containing the infos is open at front, it does the job.

Applescript:

tell application "Safari" to tell document 1
   set theSource to its source
end tell

set asList to my decoupe(theSource, ">Raw<")
set nbURLs to (count asList) - 1

repeat with i from 1 to nbURLs
   set aURL to paragraph -1 of item i of asList
   set aURL to (text items 1 thru -2 of aURL) as text
   set aURL to item 2 of (my decoupe(aURL, "href="))
   set aURL to (text 2 thru -1 of aURL) as text
   set nameString to item -1 of my decoupe(aURL, "/")
   set aURL to "https://gist.githubusercontent.com" & aURL --EDITED a typo now corrected
   
   tell application "Safari"
       open location aURL
       set maxi to 1000
       set cnt to 0
       repeat maxi times
           set cnt to cnt + 1
           if exists window aURL then exit repeat
           delay 0.2
       end repeat
       if cnt = maxi then error "The download process failed"
       set theData to get text of document aURL
       close document aURL
   end tell
   -- not sure that this kind of “cleaning” is really useful
   set nameString to my remplace(nameString, {space, quote, "/"}, "_")
   set {itsName, itsExt} to my decoupe(nameString, ".")
   set theFile to ((path to desktop as text) & itsName & "." & itsExt) as «class furl»
   
   set theFileRef to open for access theFile with write permission
   write theData as «class utf8» to theFileRef
   close access theFileRef
   
end repeat

#=====

on decoupe(t, d)
   local oTIDs, l
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
   set l to text items of t
   set AppleScript's text item delimiters to oTIDs
   return l
end decoupe

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)

on remplace(t, d1, d2)
   local oTIDs, l
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
   set l to text items of t
   set AppleScript's text item delimiters to d2
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end remplace

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 15 janvier 2020  21:54:45

Last edited by Yvan Koenig (2020-01-16 04:08:43 am)

Offline

 

#6 2020-01-15 05:07:55 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 333

Re: My first AppleScriptObjC

Thanks Yvan.

I have only 1 things to say... and it's my fault to not check your code before I run it.
I have to check my history in Safari... because your code close 4 document window for me.
That is... maybe 500 tabs... smile) haha...

Maybe a better way would be to open tabs and close tabs.

That said, beautiful coding.

If I understand correct are you searching for >Raw< in source to get the links.

I need to study your code more... and se what it does do for every line.

Regards.


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.

Offline

 

#7 2020-01-16 03:11:40 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4568

Re: My first AppleScriptObjC

When I tested, I had three windows open at front (I never use tabs)
-- a french web site mac4Ever
-- macScripter
-- the page you gave as example :  <https://gist.github.com/kaustavha/25388d53d39b55af511d>

I got 4 files on my Desktop :
out.applescript
example.js
email.txt
Readme.md

I had the same windows open in Safari.

I'm wondering about how I posted a script with a typo building a wrong link.
I edited the message #7 so that it create the correct links.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 16 janvier 2020  10:10:55

Offline

 

#8 2020-01-16 04:20:40 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4568

Re: My first AppleScriptObjC

I edited the script in message #7.
It no longer point the window/document by the index 1 but by its name : aURL.
This way I guess that there will no longer  be problems with tabs.

For tests, edit it this way :
true code

Applescript:

   if cnt = maxi then error "The download process failed"
       set theData to get text of document aURL
       close document aURL
   end tell

patched for tests :

Applescript:

       if cnt = maxi then error "The download process failed"
       set theData to get text of document aURL
       --close document aURL
       name of documents
       exit repeat
   end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 16 janvier 2020  11:20:20

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)