Thursday, August 13, 2020

#1 2020-07-23 03:29:25 am

Registered: 2019-10-23
Posts: 333

Safari Automation NSImageName

I got a idea I would like to try out... from Takaaki example.

It involve as follow.

Safari check the title of the document and set it to variable.
ex. … nguage=occ

Then it ask for the first word of theTitleName

ex. NSImageNameActionTemplate

We then use ASOC to save a png image on the desktop.

I have try to make a variable inside a method but it didn't work. So the only option I see now
to make a lot of if else statement. Other Idea I have but I have not try yet is to extract every link
get the document title and ask for the first word of evert document title. Build a repeat loop
save every NSImageNameXXXX.png to the desktop.

Here is the code so far...

Open Safari to this URL … nguage=occ
Run the AS Script


use framework "Foundation"
use scripting additions

tell application "Safari" to tell document 1
   set theDocumentTitle to do JavaScript "document.title"
end tell

set theNSImageName to (its first word of theDocumentTitle)

set theOut to POSIX path of (path to desktop as text) & theNSImageName & ".png"

if theNSImageName = "NSImageNameActionTemplate" then
   set theImage to (current application's NSImage's imageNamed:(current application's NSImageNameActionTemplate))
end if

if theNSImageName = "NSImageNameActionTemplate" then
   saveNSImageAtPathAsPNG(theImage, theOut)
   log missing value
end if

on saveNSImageAtPathAsPNG(anImage, outPath)
   set imageRep to anImage's TIFFRepresentation()
   set aRawimg to current application's NSBitmapImageRep's imageRepWithData:imageRep
   set pathString to current application's NSString's stringWithString:outPath
   set newPath to pathString's stringByExpandingTildeInPath()
   set myNewImageData to (aRawimg's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value))
   set aRes to (myNewImageData's writeToFile:newPath atomically:true) as boolean
   return aRes
end saveNSImageAtPathAsPNG

I was able to get Safari to output every link from the page in the console
with JavaScript. I use getElementsByClassName with the specific class that
Apple use on that site.

So now comes the question, how could I output a JavaScript Array to AS List, is that possible.
The website from Apple is the same as above.


set getDocumentLinks to "function getDocumentLinks () {
// var links = document.getElementsByTagName('a');
var links = document.getElementsByClassName('link has-adjacent-elements');
for(var i = 0; i< links.length; i++) {

set getDocLinks to {}
tell application "Safari" to tell document 1
   set getDocLinks to (do JavaScript getDocumentLinks) as list
end tell

I getting closer I see a beautiful valley of sunshine coming to me... smile


use framework "Foundation"
use scripting additions

set theArrayToString to "function arrayToString() {
const arr = [1,2,3,'Adam','Eve','Bob','Shane','Steve'];
// console.log(arr.toString());
return (arr.toString());

tell application "Safari" to tell document 1
   set theArrayToString to do JavaScript theArrayToString
end tell

return my im_componentsSeparatedByString(theArrayToString, ",")

on im_componentsSeparatedByString(inputString, seperator)
   set theString to current application's NSString's stringWithString:inputString
   return (theString's componentsSeparatedByString:seperator) as list
end im_componentsSeparatedByString

Last edited by Fredrik71 (2020-07-23 04:04:24 pm)

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



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)