So I'm new to applescript and I'm trying to create an app that will open microsoft word and then save the document every once in a while automatically and then stop running once I close Word. This is my code so far:
activate application "Microsoft Word"
if application "Microsoft Word" is running then
repeat
delay 5
tell application "Microsoft Word" to save front document
end repeat
end if
It works up until the point that I quit word. Even though I have it set to end if word isn't running it still tries to save to word and then I get an error message. Another interesting thing to note is that it even says end tell in the script once I quit word.
What am I doing wrong?
Save this as a stay-open application:
activate application "Microsoft Word"
on idle
say "idle"
if application "Microsoft Word" is running then tell application "Microsoft Word" to save front document
return 5 -- in seconds
end idle
Related
I am new to AppleScript and tried an AppleScript solution to open up a tab and load a webpage:
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
But if tab 2 doesn't exist, it won't do anything.
I then tried to solve it by checking whether an URL exist in any tab:
tell application "Google Chrome"
repeat with w in windows
set i to 1
repeat with t in tabs of w
if URL of t starts with "https://mail.google" then
set active tab index of w to i
set index of w to 1
return
end if
set i to i + 1
end repeat
end repeat
open location "https://mail.google.com/mail/u/0/#inbox"
end tell
But it is sort of an overkill, and it opens up a new tab, rather than opening up a tab 2.
I then tried
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
open tab 2 -- added this line
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
but no matter it is open tab 2 or activate tab 2 or start tab 2, it wouldn't work. How can we make it work and, in general, how to find out the vocabularies or verbs that we can use?
open tab 2 is the problem. If a browser window exists and you want to create a new tab and set its URL, then:
tell application "Google Chrome" to ¬
if exists front window then ¬
make new tab at end of ¬
tabs of front window ¬
with properties ¬
{URL:"https://www.example.com"}
If a browser window exists and you want to change the URL of tab 2, then:
tell application "Google Chrome" to ¬
if exists front window then ¬
tell front window to ¬
if (exists tab 2) then ¬
tell its tab 2 to ¬
set its URL to ¬
"https://www.example.com"
Notes:
The example AppleScript code works with Microsoft Edge too.
These examples can be expanded into normal block format using if statements to determine if Google Chrome and or Microsoft Edge exists, or are already running, and if running whether or not a window exists, etc. You just need to code it according to your needs/wants.
Have a look at AppleScript Language Guide
In Script Editor have a look at the Library for the AppleScript dictionary of any application you want to write code for. From the Window menu... Library ⇧⌘L
The example AppleScript code below shows an example of coding for the existence of either Google Chrome or Microsoft Edge and whether or not they are running when you can then write code based on the conditions. As written it favors Google Chrome but if it does exist then Microsoft Edge runs its code. To favor Microsoft Edge change the primary if statement structure to accommodate.
tell application "System Events"
set existsGoogleChrome to exists file "/Applications/Google Chrome.app"
set existsMicrosoftEdge to exists file "/Applications/Microsoft Edge.app"
end tell
if existsGoogleChrome then
if running of application "Google Chrome" then
tell application "Google Chrome"
# Do Something
end tell
else
tell application "Google Chrome"
# Do Something Else
end tell
end if
else
if existsMicrosoftEdge then
if running of application "Microsoft Edge" then
tell application "Microsoft Edge"
# Do Something
end tell
else
tell application "Microsoft Edge"
# Do Something Else
end tell
end if
end if
end if
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.
Lately I have discovered Automator (yes, finally she did that) and I wanted to make an event which runs at a regular interval using Calendar. I wanted to save a particular web page to Evernote using the Web Clipper extension in Safari.
I set up my Web Clipper to start on $, F = Full page, enter = Save.
I have come so far as to actually have an event which works by:
Creating a new document in Automator of the type Calendar
Adding "Get Specified URLs" with the URL I want
Adding "Display Webpages"
Adding "Run AppleScript" with the following code - I'm a total noob at AppleScript so you might say I could have done it in a better way ... then do tell ;) ...:
tell application "Safari" to activate
delay 5
tell application "System Events"
keystroke "$" -- key code 10 = Activate Web Clipper (custom shortcut)
end tell
delay 1
tell application "System Events"
keystroke "f" -- key code 3 = Full page saved by Web Clipper
end tell
tell application "System Events"
key code 36 -- works to save page, however, 'keystroke enter' does not
end tell
5.Saving the document in a Calendar event and set it up to repeat.
I found some help here with a list of key code values, however, I couldn't find "enter" in the list. I used a little free app called Key Codes instead to figure out that enter has the key code 36.
I would rather be able to use keystroke, since it is easier to read than some number. Can anyone help?
i'm not sure this will do exactly what you want but maybe just keystroke return ?
tell application "Safari" to activate
delay 5
tell application "System Events"
keystroke "$" -- key code 10 = Activate Web Clipper (custom shortcut)
delay 1
keystroke "f" -- key code 3 = Full page saved by Web Clipper
keystroke return
end tell
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
I've been appointed the task of creating an applicaton that must
Display a dialog saying "The webpage has finished loading." when a webpage finishes loading
Determine how many items are currently in the process of downloading
For number 1, I've tried doing if the URL of the front document is "http://applescript.weebly.com" then, but the then part always runs even if the webpage doesn't load!
For number 2, I've tried this...
tell application "Safari" to get every item of window "Downloads"
But this returns references to every item, even the ones that havae already been downloaded!
Conclusion: I need help. :S
Display a dialog saying "The webpage has finished loading." when a webpage finishes loading.
There is no way to do this with just pure AppleScript. However, you can use a combination of AppleScript and javascript...
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
end repeat
display dialog "The webpage has finished loading."
end tell
WARNING: If the webpage doesn't load for some reason, the script will be stuck forever in an infinite repeat loop.
Determine how many items are currently in the process of downloading.
When you download files, they are temporarily given the name extension download, so AppleScript can tell the Finder to get the files with that extension and create an alert/dialog:
set the download_count to 0
set the download_folder to (path to downloads folder) as alias --or wherever the items are being downloaded
tell application "Finder" to set the download_count to the count of (every item of the download_folder whose name extension is "download")
if the download_count is 0 then
display dialog "There are currently no downloads in progress."
else if the download_count is 1 then
display dialog "There is currently one download in progress."
else
display dialog "There are currently " & the download_count & " downloads in progress."
end if
P.S. Thanks for honoring my web site!
I wrote the script below in AppleScript Editor to open my browser, display my ISP in the URL, go through the login process and connect to the internet. Is working perfectly. I want to add or modify it to ping "www.google.com" every hour or so, and if my connection has dropped to repeat the process below again, which will connect my computer back to the internet.
I'm just learning this script writing thing and don't have a very good undersatnding of how it works yet, so if anyone can help please expalin it in simple terms for me. Very much appreciated.
tell application "Safari"
activate
tell application "System Events"
open location "https://basrah.uswicom.com/login.php"
delay 1
keystroke return
delay 3
keystroke tab
keystroke (ASCII character 29)
delay 1
delay 1
keystroke tab
delay 1
keystroke "123456789"
delay 1
keystroke return
delay 1
keystroke return
end tell
end tell