Get process id after spawning new application via applescript(osascript) using tell - macos

tell application "Google Chrome"
make new window
open location "https://google.com"
end tell
The above command spawns up an instance of google chrome. I want to get the process id of this process so that I can kill it later. Please note that I am using Google chrome for example, but I can spawn up any process any number of times. Just need to get the process id.

You can store a reference to the window you make in a variable, and use that variable to close it later. The following opens two windows (to Google and Yahoo), waits three seconds, the closes the Google window:
tell application "Google Chrome"
set windowGoog to make new window
tell windowGoog to open location "https://google.com"
set windowYah to make new window
tell windowYah to open location "http://Yahoo.com"
end tell
(*
You can do whatever you need to do here. I've added a three second delay just
to give a sense of time, but that's just for show.
*)
delay 3
tell application "Google Chrome"
close windowGoog
end tell
If you want to keep a reference to the window across multiple runs of the script (e.g., you run the script once to open the window, then run it again later to close it) make the variable a property, and use if statement to check if it has a value, like so:
(*
These two lines set 'windowGoog' and 'windowYah' to 'missing value' on the
first run, and then remember whatever value you set until the next time
you recompile the script
*)
property windowGoog : missing value
property windowYah : missing value
tell application "Google Chrome"
if windowGoog is missing value then
set windowGoog to make new window
tell windowGoog to open location "https://google.com"
else
close windowGoog
set windowGoog to missing value
end if
if windowYah is missing value then
set windowYah to make new window
tell windowYah to open location "http://Yahoo.com"
else
close windowYah
set windowYah to missing value
end if
end tell

Related

How to open a URL with AppleScript only if no other URL checked is the right one?

I'm looking for solution but my searches was not good so far
This is my code
tell application "Google Chrome"
if it is running then
repeat with t in tabs of windows
tell t
if URL starts with "https://keep.google.com/" then
delay 1
activate
else
open location "https://keep.google.com/"
delay 1
activate
end if
end tell
end repeat
else
activate
open location "https://keep.google.com/"
delay 1
activate
end if
end tell
tell application "Google Chrome" to activate
return ""
I'm able to check the URL, but for each URL that is not Google Keep I open a new URL. So if I have Gmail, Youtube and Keep opened, I open more two keeps URLs. How to open only if no one is the right URL?
First of all, tell application "Google Chrome" in the following code snippet at the start of your code block will launch Google Chrome if it's not running.
tell application "Google Chrome"
if it is running then
So, technically that's not the proper way to use the running property in the next line because as you presently have it coded, Google Chrome is always running when if it is running then is processed.
Here is an example of how I'd rewrite your code to make proper use of the running property and only open the target URL is it not already open.
Example AppleScript code:
set theURLsList to {}
set theURL to "https://keep.google.com/"
if running of application "Google Chrome" then
tell application "Google Chrome"
repeat with t in tabs of windows
copy URL of t to end of theURLsList
end repeat
if theURLsList does not contain theURL then
open location theURL
activate
end if
end tell
else
tell application "Google Chrome"
open location theURL
activate
end tell
end if
Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways to accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

AppleScript, quit an app by id, safely

It's possible to run multiple instances or copies of the same app on a Mac. But AppleScript can't seem to identify them separately. Say my app is "FileMaker Pro" and I have multiple copies of it running. Having AppleScript tell "FileMaker Pro" to quit, I believe quits the first one that ran, which might not be the one I want it to quit.
I want to write a script that first will identify the frontmost application, then go off and do some other stuff (which might bring other apps to the front) then SAFELY quit the original frontmost application that it identified at the start.
Some googling I've done has found suggestions where I identify the the frontmost application by process id and then
do shell script "kill ..."
but from what I can tell "kill" doesn't ask to save changes, etc. (so it doesn't SAFELY quit").
I want this script to do exactly what the AppleScript quit command or manually choosing quit from the file menu would do, including ask to save changes or whatever else.
Is this possible? If so how?
For a copy of an application: it's possible by using the path of the application instead of the name.
tell application "System Events"
tell (first application process whose frontmost is true)
set x to its application file -- get the path of this application (the result is an alias of "System Events")
end tell
set thisAppPath to path of x -- get the path (a string)
end tell
--- *** go off and do some other stuff ****
---
--- SAFELY quit the original frontmost application that it identified at the start.
tell application thisAppPath -- the path must be a string
try -- *** use 0 to no limit ***
with timeout of 240 seconds -- a time limit is given to the user to save changes, etc.
quit
end timeout
on error -- (timeout error): the user do nothing because the application is still open
-- do something
end try
end tell
--
-- script after the application quits
--

Error number -128 Applescript

I have a simple applescript that opens Safari, opens a web page 100 times (long story) and then waits 300 seconds to close Safari.
Here is the code:
set theURL to "https://sites.google.com"
tell application "Safari"
activate
repeat 100 times
try
tell window 1 to set current tab to make new tab --with properties {URL:theURL}
set URL of document 1 to theURL
on error
open location theURL
end try
end repeat
end tell
delay 300
tell application "Safari"
quit
end tell
The first part of the script runs famously. When I get past the delay, I am getting an error:
error "Safari got an error: User canceled." number -128
I tried to just run it without opening multiple web pages Just activate Safari, wait 10 second and quit. I get the same problem.
Has anyone dealt with this?
I'm not getting the error you describe. However I am seeing a bug where Safari is not activating if the application was not already running when I run the code.
This code seems to work as you'd expect...
set theURL to "https://sites.google.com"
tell application "Safari" to activate
tell application "Safari"
activate
repeat 2 times
try
tell window 1 to set current tab to make new tab
set URL of document 1 to theURL
on error
open location theURL
end try
end repeat
end tell
delay 5
tell application "Safari"
quit
end tell

Applescript opening wrong version of OS X app

My script is opening an old version of the app (which I wrote). I have searched the HD and cannot find the old app. The new version is in the applications folder. The script is in the scripts folder.
I made a new folder and put the script and the new app in it. Running the script still opens the wrong app. App2 is the problem child.
Where is the wrong path coming from?
How to:
Point the script at the correct app? I don't want a path in the script.
Find the old app and delete it?
property checkInterval : 5 -- Number of seconds between checks. Adjust to requirement.
global App1WasOpen, GUIScriptingWasEnabled, previousText
on run
-- When the script starts up, note if App1 is running and GUI Scripting is enabled.
tell application "App1" to activate
tell application "System Events"
set App1WasOpen to (application process "App1" exists)
set GUIScriptingWasEnabled to (UI elements enabled)
end tell
-- Enable GUI Scripting if nec. and give the 'previousText' variable an initial value.
switchGUIScripting(true)
set previousText to ""
end run
on idle
-- Each time the script's polled by the system, check that App1's open.
tell application "System Events" to set App1IsOpen to (application process "App1" exists)
if (App1gIsOpen) then
set App1WasOpen to true
-- Get the latest value for 'beam'.
tell application "App1"
set hdg to getHeading
set beam to hdg as string
end tell
if ((count beam) < 3) then set beam to text -3 thru -1 of ("000" & beam)
-- If it's changed since the last check, enter it into App2 and keep it for later checks.
if (beam is not previousText) then
--display dialog "Opening App2" buttons {"OK"}
tell application "App2" to launch
--activate application "App2"
tell application "System Events"
set startTime to current date
repeat until exists (text field 1 of window "App2" of application process "App2")
if (current date) - startTime is greater than 5 then
error "Could not find text field 1 of window AppB of application process App2"
exit repeat
end if
delay 0.2
end repeat
tell application process "App2"
try
set value of text field 1 of window "App2" to beam
end try
end tell
end tell
--tell application "App1" to activate
set previousText to beam
end if
-- Request the next 'idle' event in 'checkInterval' seconds' time.
return checkInterval
else if (App1WasOpen) then
-- App1 was open at the last check, but isn't now. Tell this script applet to quit.
quit
-- It'll actually quit on the next idle event, so request a short interval.
return 1
end if
end idle
-- Unless GUI Scripting was already on when the script started to run, turn it on or off as per the parameter.
on switchGUIScripting(onOff) -- 'onOff' is 'true' for on, 'false' for off.
if (not GUIScriptingWasEnabled) then
tell application "System Events"
activate
set UI elements enabled to (onOff)
end tell
end if
end switchGUIScripting
-- When this applet's told to quit, restore the previous GUI Scripting state before it does.
on quit
switchGUIScripting(false)
continue quit
end quit
Followup:
I used "Show in Finder" on the app's icon in the dock to point me to 17 Xcode archives, which I deleted. Now the script has a recurring message of "An error of type -10660 has occurred", and App2 never opens.
I opened the script editor, copied the code to a new script window and compiled it with a different name. Still doesn't open App2.
Try finding the Applications id with:
set appID to get id of application "/Applications/Safari.app"
Once you have found it, refer to the id rather than
tell application id "com.apple.Safari" to launch

Open URL in new Safari tab with AppleScript

Is it possible to use AppleScript to open a link in a new tab in Safari?
This will work:
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
end tell
end tell
I think this also does what you asked for, but it is much shorter and is less browser-specific:
do shell script "open http://www.webpagehere.com"
This will open the specified URL in your default browser. And if you explicitly want to open it in Safari, use this:
do shell script "open -a Safari 'http://www.webpagehere.com'"
This should usually create a new tab and focus it (or focus an existing tab if the URL is already open):
tell application "Safari"
open location "http://stackoverflow.com"
activate
end tell
It opens a new window if "Open pages in tabs instead of windows" is set to never though.
tell application "System Events" to open location doesn't work with some URLs that contain non-ASCII characters:
set u to "http://ja.wikipedia.org/wiki/漢字"
tell application "System Events" to open location u
--tell application "Safari" to open location u
--do shell script "open " & quoted form of u
This opens a new tab even when new pages are set to open in windows:
tell application "Safari"
activate
reopen
tell (window 1 where (its document is not missing value))
if name of its document is not "Untitled" then set current tab to (make new tab)
set index to 1
end tell
set URL of document 1 to "http://stackoverflow.com"
end tell
tell application "System Events" to tell process "Safari"
perform action "AXRaise" of window 1
end tell
set index to 1 doesn't raise the window, but it makes the window appear as window 1 to System Events, which can AXRaise it.
I've been using the following script to open hundreds of docs into tabs in a single window.
tell application "Safari"
tell window 1
make new tab with properties {URL:"http://foo.com/bar"}
make new tab with properties {URL:"http://foo.com/baz"}
end tell
end tell
But that no longer works in Safari 5.1 on Lion. It would open the new tab, but it wouldn't load the URL provided in the properties glob. I modified it to the following, which now works:
tell application "Safari"
tell window 1
set URL of (make new tab) to "http://foo.com/bar"
set make new tab to "http://foo.com/baz"
end tell
end tell
Code:
tell application "System Events"
tell application "Safari" to activate
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
tell application "Safari"
set URL of document 1 to "http://www.stackoverflow.com/"
end tell
One problem is that this only works if the system's language is set to English.
It's been a while since a new answer's been posted here. I think this is the optimal way to do this. It will open Safari if it's not open, create a new window if there are no windows open, and add the tab to the current (or newly created) window.
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:theURL}
on error
open location theURL
end try
end tell
I can't comment :-/ so I will answer to say that Tim's answer (above) works as of OS X 10.8.5. This one-line version of his script also works:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
Arrgh -- the one line is overflowing. Here it is without the code tags:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
I ended up using automator to do this which was much easier and it works.
You can try following approach:
//make Safari window active and topmost
tell application "Safari" to activate
//start communication with Safari
tell application "Safari"
tell window 1
//create new tab and open specified URL
tab with properties {URL:"https://url.com"})
//make tab active
set visible to true
end tell
end tell
Also u can combine usage of Apple script within FastScript (free for 10 shortcut)
To add your script - just save script in /Library/Scripts. After you will be able to set some shortcut for new script.
If you want to open new Window than new tab u can play within next:
tell application "System Events"
tell process "Safari"
click menu item "New window" of menu "File" of menu bar 1
end tell
end tell
Note: you need to allow AppleScript to use specialCapabilities in security settings in this case.
Not the shortest solution but also works, and not only in English ...
tell application "Safari"
activate
end tell
tell application "System Events"
set frontmost of process "Safari" to true
keystroke "t" using {command down}
end tell
set myURL to "anyurl.html"
delay 2
tell application "Safari" to set the URL of the front document to myURL
Worked for me in Safari v.11
tell application "Safari"
tell window 1
make new tab with properties {URL:"https://twitter.com"}
end tell
end tell
I found a way to open a new tab in the background with Safari.
tell application "Safari"
set the URL of (make new tab in window 1) to "your.url.net"
end tell
During the time I wrote this answer I made this
tell application "Safari"
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
end try
end tell
New version
tell application "Safari"
repeat
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
if theURL is "" then exit repeat
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
display dialog "Do you want to open a new tab?" buttons {"Yes", "No"} default button "Yes"
if button returned of result is "No" then exit repeat
end try
end repeat
end tell
Any suggestions will be appreciate
Best regards

Resources