Applescript to open Safari - safari

(newbie here) I'd to be able to save an applescript file as an app to use to open Safari, I cobbled together this from some old posts I found, it's not exactly what I want but it worked ok, however today it stopped working giving an error "The variable theURL is not defined."
If anyone could help me out with a better script that would work on Monterey I would be really grateful. Also, when I save as an app - should I select 'Stay open after run handler' or 'Run-only'? It just needs to open Safari when it's clicked, I don't need it to really open any particular page. Thanks in advance!
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:"https://google.com.au"}
on error
open location theURL
end try
end tell

I cobbled together this from some old posts I found:
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:"https://google.com.au"}
on error
open location theURL
end try
end tell
A lot (virtually all) applescripts you see online make overuse of try blocks, which, apart from being completely unnecessary, are problematic in a number of ways, including making debugging more difficult. I'd advise taking out the line try, and everything including and between the lines on error, and end try.
Without those lines, you'll probably get a different error message, which was being masked by the try block. It most likely that window 1 didn't exist, which would be the case if Safari had already been running and didn't have any windows open. It would have then thrown an error, forwarded the script on to the on error block, where open location theURL then, itself, threw an error because theURL hasn't been defined.
I don't need it to really open any particular page
Then all you need is this:
tell application "Safari" to activate
If Safari is already running without any open windows, all this does is bring Safari to the front; it doesn't open a new window for you. If you need to create a new window you can usually do this:
tell application "Safari"
make new document
activate
end tell
However, this will create a new window even if there's already a window open, which might not be what you want. To create a window but only if a window doesn't already exists, you can do this:
tell application "Safari"
if documents = {} then make new document
activate
end tell
I suspect this is the script you will want to use.
Should I select 'Stay open after run handler' or 'Run-only'?
No and no. The first option prevents your app from quitting without explicit instruction to do so, which isn't needed in this case: you simply need your app to open, start Safari, then quit. The last option prevents you from making any edits to the script in the future, which, again, probably isn't what you want.

Related

Using applescript to open a page in notion (on Mac)

I am trying to set up a script to open a specific page in the notion app. So far I've tried the following:
tell application "Safari"
activate
tell window 1
set current tab to (make new tab with properties {URL:"notion://www.notion.so/Focus-Mode-5eca2183453748d3b5acaa8e7292ef83"})
end tell
end tell
but Safari requires that you give permission to open the app. Is there anyway to get around this? If not, is there anyway to open the page in Notion without having to go through Safari?
It can be even simpler:
I was playing with your solution and found out that even though you can't finde those commands in the dictionary, they still exist:
tell application "Notion"
open location "notion:page:Focus-Mode-5eca2183453748d3b5acaa8e7292ef83"
end tell
I figured out a solution - this was painfully obvious...
tell application "Notion"
open location "notion://www.notion.so/Focus-Mode-5eca2183453748d3b5acaa8e7292ef83"
end tell

Applescript: how to "tell" an app in the background?

I'm using Chrome to get the HTML of a webpage which is generated by javascript. The applescript which does all this needs to run every 2 minutes. Everything is working perfectly, except that I obviously need Chrome to do this completely in the background. My script contains the following uses of Chrome (as well as a block to set theTab, which doesn't seem to ever cause Chrome to come to the front):
set URL of theTab to theURL
set isLoading to (loading of theTab)
execute front window's active tab javascript javascriptLocation
set theSource to execute front window's active tab javascript "document.documentElement.outerHTML"
Putting this line:
tell application "Finder" to set visible of process "Google Chrome" to false
after each of the above lines either produces no hiding at all, or at best Chrome flashes onscreen and then goes away. I find this very distracting.
Is there any way to have an application run reliably and permanently in the backround? Or, failing this, is there an invisible way to get javascript executed server-side so an applescript can get hold of its generated source?
Chrome 66.0.3359.181 running on Mac OS 10.11.6, Applescript 2.5.
The following is not an answer to the question I posted, but it is an answer to the problem I was trying to solve which is why I posted the question.
As stated in my question, I need to get hold of the HTML which some javascript generates on a site (which is not under my control). I can't do a client-side scrape because of CORS restrictions on the site. I tried the cross-domain tools listed here and couldn't get them to work.
So I was using Chrome's applescript command, execute, to first execute the javascript (to produce the HTML), and then a second time to grab hold of the HTML with document.documentElement.outerHTML. But having Chrome flash onto the screen every 2 minutes throughout the day was doing my head in.
Turns out Chrome can also run in headless mode, from the command line, and just happens to have an option to run javascript and return the generated HTML!
So my code got a whole lot simpler and I don't have to have Chrome in my apps list all the time. Happy coder am I :-)
Here's the one line that gets me the HTML generated on the site I need:
set theSource to (do shell script ((quoted form of POSIX path of googlePath) & " --headless --dump-dom " & theURL))
Thanks, once again, #matt. I'd never heard of headless mode and would never have found this without your suggestion of PhantomJS!

Unhandled win32 exception occured in the application

I have an application in vb.net that I'm testing out in Windows 10 and I seem to be getting an error (Images below). This app works flawlessly in Windows 7, and it actually works without any issues in Windows 10, the problem is, when I exit the application is when I get the error.
The way it's structured, is if I run it from IDE, i first see a Login Windows where user logs in and then goes to MENU. If it's run in our environment, user does not have to log in, so the log in form never appears, it goes directly to MENU.
Everything works great, until I go to EXIT Application, where it gets all messy, this is the code from EXIT button...
Dim Answer as Integer
Answer = MsgBox("Are you sure you wish to Close the application ?", MsgBoxStyle.YesNo)
If answer = vbYes Then
End
End If
These are the errors I get:
First I get this error, clicking on CLOSE PROGRAM closes it completely, if I click debug I get the below windows....
With the 2nd error it shows that I actually have VS2010 and VS2012, and it lets me debug. The issue is, the source code is in TFS, and it just so happens that I can't access the TFS from my windows 10 machine, (only from Win 7). So I can't debug it. But is there a reason why this is happening only in windows 10?
I even went as far as doing Me.Close() before END to make srue that the form is closed. And again, it works fine in Win 7, but win 10 it gives me the same problems.
Using "End" to close a program can be problematic; the comments and answer to this SO question explain why that is. As for the second issue that popped up once using Application.Exit(), that is a simple case of your program referencing multiple assemblies that have function calls with the same name. In this case, both the explicitly imported Microsoft.Office.Interop.Excel and implicitly imported System.Windows.Forms have "Application.Exit()" members. Since you have explicitly imported Excel, the compiler goes with that one when it's forced to decide which Exit() to use, which throws an error because of the context it's being used and doesn't actually close the program. To rectify that, all you have to do is explicitly tell the compiler which Exit() you want to use by replacing
Application.Exit()
with
System.Windows.Forms.Application.Exit()

AppleScript handler not working when called from within tell Finder block

Learning to work with handlers in AppleScript I've run into an issue. Currently I have trimmed my application to call a handler when ran but currently I get an error:
error "Script Editor returned error -1708" number -1708
When I compile I have no issues. When I try to research for a solution I'm unable to find a reason why this doesn't work under the application Finder. To memory, I recall the on run must be declared last in the application.
the code:
on checkForRebels(tieFighter, starDestroyer)
display dialog "dark enough" as text
end checkForRebels
on run
tell application "Finder"
set the starDestroyer to (choose folder with prompt "Please select the Destroyer")
set tieFighter to items of starDestroyer
checkForRebels(tieFighter, starDestroyer)
end tell
end run
I tried searching for
applescript items throw error when passing to handler
but I returned Apple's Working with Errors which doesn't answer my question. When I review Apple's documentation About Handlers I don't see anything disclosing what the issue should be.
When I modify my script to:
on checkForRebels(tieFighter, starDestroyer)
display dialog "dark enough" as text
end checkForRebels
on run
tell application "Finder"
set the starDestroyer to (choose folder with prompt "Please select the Destroyer")
set tieFighter to items of starDestroyer
end tell
checkForRebels(tieFighter, starDestroyer)
end run
it works without any issue but why does the handler checkForRebels() not work in application Finder's tell block? When I call a handler do I have to always do it outside of an application for it to work?
When you are calling a handler from within a tell block or other handler, you need to specify "my", otherwise it is looking for that command in the Finder dicitonary.
my checkForRebels(tieFighter, starDestroyer)
From the Applescript Language Guide Calling Handlers in a tell Statement:
To call a handler from within a tell statement, you must use the
reserved words of me or my to indicate that the handler is part of the
script and not a command that should be sent to the target of the tell
statement.

In OS X Lion, asking the Finder for the current selection, returns the wrong file

I've confirmed that this is only happening in Lion. I've uncovered what appears to be a bug where you need to focus a window twice in order for Finder to return the correct result over the scripting bridge or AppleScript. This only happens when opening a folder from the desktop or another space.
Here's how to duplicate:
Close, hide or minimize all windows until the desktop is showing.
Open any folder on the desktop, a new finder window will appear. Select any file in that finder window by clicking on it once.
Open your AppleScript Editor and run the following script:
tell application "Finder"
return selection
end tell
The script will return only the path to the folder on the desktop, not the selected file in the finder window.
If you refocus the window, or click on the file again, everything works as expected and the script returns the correct path.
This seems like a bug in Lion. Snow Leopard returns the correct path 100% of the time.
I've tried the following methods with no success:
Using the scripting bridge and https://github.com/davedelong/BetterInfo/blob/master/Finder.h
Using "System Events" apple script to click "Edit"->"Copy"
Using AppleScript to get the current selection from the finder.
All seem to have have the same issue.
Am I missing something obvious or is this truly a problem with Lion?
Wow, I can confirm (at least through applescript) most of what you describe. I was getting an empty list returned, not even the desktop path you mention. I was testing this using a script in the applescript menu. The only way I could get the selection from a fresh Finder window was like this...
tell application "System Events" to activate
tell application "Finder"
activate
set a to (get selection) as text
display dialog a
end tell