I am trying to do the following:
Open a new Browser in Safari. Load a specific URL in that new window.
Then send a javascript to that window.
I can’t seem to make this happen. I can’t figure how to make a handle for the new window, so I can use that handle when I’m sending javascript to it.
This does work to open the new window: set my_reporter to make new document with properties {URL:"https://example.com"}
But then when I do: do javascript "javascript" to my_reporter
That of course, does not work.
I can’t just use “document 1” to specify the window, I have a ton of safari browsers open at all time, I need to be more specific than that.
do JavaScript v : Applies a string of JavaScript code to a document.
do JavaScript text : The JavaScript code to evaluate.
[in document or tab] : The tab that the JavaScript should be evaluated in.
→ any
tell application “Safari”
set aRes to do JavaScript “document.title;” in front document
end tell
Safari’s "do JavaScript " requires “document”.
You have to give some document with it.
Window object does not respond to “do JavaScript” command.
If you want to access the frontmost window, you can use “front document” ( = window 1).
If you want to use “URL”, you have to make new document with some URL.
How do I set a variable to the new document, so i can pass that to the do javascript command directly, so the script doesn’t rely on the window i want to work with being the frontmost window?
set aURL to "http://piyocast.com/as/"
tell application "Safari"
set d to count every window
if d = 0 then
make new window
tell front document -- = window 1
set URL to aURL
end tell
else
if aURL is not equal to "" then
tell front document -- = window 1
set URL to aURL
end tell
end if
end if
end tell
Other basic and advanced know hows are in my ebook.
If you would consider a beta 3rd party framework, it would look something like this:
use safariLib : script "core/safari"
set safari to safariLib's new()
set safariTab to safari's newTab("https://www.example.com")
safariTab's runScript("console.log('Holler');")
-- set safariTab to safari's findTabByUrlPrefix("https://www.apple.com")
English translated version made very very tiny sales. So, I don’t have a plan to translate them.
You can use Google Translate or a Chrome extension to translate PDFs into other languages.
Google Translate
1 Go to Google Translate
2 Click Documents at the top
3 Choose the languages to translate to and from
4 Click Browse your computer
5 Select the PDF you want to translate
6 Click Translate
7 Click Download translation to save the translated document
Chrome extensions
• PDF Translator: Translate PDFs, Word, Excel, PowerPoint, and more
• Document Translator: Translate PDFs, Word, Excel, PowerPoint, and more
• Instant Multilingual PDF/HTML/TXT Translator: Translate PDFs, HTML, and TXT
You can also translate text in Chrome by:
1 Highlighting the text you want to translate
2 Right-clicking the highlighted text
3 Selecting Translate selection to [Language]
Since I had to google around quite a bit to find this framework and I guess that not everybody knows where to find it, let me leave some pointers here:
Additionally, note the following paragraph from the Read Me, especially its last sentence:
How to Install and Run the Project
Checkout this project: git clone GitHub - roycetech/applescript-core
Run make install. It will install the essential libraries under ~/Library/Script Libraries. It will also install basic sounds and property files under ~/applescript-core/. See the Makefile for more information.
To test that it works, open the files inside examples using Script Editor and run them.
Optionally install individual wrappers as needed. (e.g. make install-safari)
You can create a document with a specific URL, then grab the first tab from the resulting window.
do JavaScript can take a document or a tab as its target:
set myDoc to missing value
tell application "Safari"
if myDoc is missing value then
make new document with properties {URL:"https://www.apple.com/"}
-- record the tab
set myDoc to tab 1 of window 1
end if
---
set JavaScriptResult to do JavaScript "document.title" in myDoc
log JavaScriptResult
end tell
the tab will remain persistent/valid as long as it’s not closed, so you can continue to target it no matter what else the user is doing.