Working in a Safari Browser

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” command form is like following

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

This works if Safari window exists.

Yes, I can get the script to work like this. How can I get doscript to run in a window/browser/document/url that I just opened as a new window?

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.

OK?

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?

How about this?

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.

https://piyomarusoft.booth.pm/items/3664077

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")

Do you have plans to have an english version? I’m going to need at least 2 years to learn Japanese

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]
1 Like