test_two() and test_three work(), but test_one() does not. Why is that? How do I pass the argument into the handler and use it as a local variable?
set the_application to "Safari"
test_one(the_application)
on test_one(the_application)
tell application the_application
make new document with properties {URL:"http://www.stackoverflow.com"}
end tell
end test_one
# ----
test_two()
on test_two()
tell application "Safari"
make new document with properties {URL:"http://www.stackoverflow.com"}
end tell
end test_two
# ----
set the_application to "Safari"
test_three(the_application)
on test_three(the_application)
tell application the_application
activate
end tell
end test_three
Here is my other working solution, maybe, better than my first one:
set the_application to "Safari"
test_one(the_application)
on test_one(the_application)
set theScript to "tell application \"" & the_application & "\"
make new document with properties {URL:\"http://www.stackoverflow.com\"}
end tell"
run script theScript
end test_one
When you want use some terms common to multiple applications, you can do it using terms from one of them. This terms will work for other applications as well. You can indicate in the code's first line the name of any application, which has command make new document with properties and property URL,
set the_application to "some Safari like application"
test_one(the_application)
on test_one(the_application)
using terms from application "Safari"
tell application the_application
make new document with properties {URL:"http://www.stackoverflow.com"}
end tell
end using terms from
end test_one
Other example (creates new folder on the desktop):
set the_application to "Finder"
test_one(the_application)
on test_one(the_application)
using terms from application "System Events"
tell application the_application
make new folder with properties {name:"TestFolder"}
end tell
end using terms from
end test_one
Related
I have a script that downloads a report from an online service, waits a specified amount of time e.g. 30secs for the file to actually download and then renames the file. The script then repeats
What I would like to do is instead of putting a static delay in the script, to create a trigger that looks for the new file and once it appears triggers the renaming portion of the script.
The downloaded file name is constant.
The current portion of the script looks like this
`
tell application "Finder" to activate
tell application "System Events"
delay Wait_Time
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
ultimately what I would like is to replace the delay Wait_Time with a something like:
repeat until file name found
Search Folder for "file name"
end repeat
select new file
rename new file to clipboard
obviously this wont work but it kind of captures my logic.
I have also tried a few other possible solutions but Im new to AppleScript and not overly confident that these approaches are suitable for my application.
The below is an example of a solution I have tried. This is just an example and im not sure if this particular code can even get me to where I need to be. I thought I would include it to show just how lost I am.
set excludes to {"Folder", "Application"}
tell application "Finder"
set search_folder to folder "Macintosh HD:Users:XXXXXXXX:Library:CloudStorage:OneDrive-Flinders:Flinders Connect Stats:Source Stats:Cisco Finesse:Agent State Summary by Interval Report"
set foundItems to (every item in search_folder whose name contains "Agent State Summary by Interval Report" and kind is not in excludes) as alias list
if foundItems is {} then return
repeat with once from 1 to 100
try
if (count of foundItems) = 1 then
tell application "Finder" to activate
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
exit repeat
end if
end try
end repeat
return
end tell
Im sure you can see I have no idea what im doing.
Anyway any help would be awesome!
I have found a solution... if anyone has ideas for improvement please let me know.
tell application "Finder" to activate
tell application "System Events"
repeat until (exists (files of folder folderPath whose name contains "Agent State Summary by Interval Report"))
delay 1
end repeat
end tell
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
end repeat
say "Finished!"
display dialog "Completed"
The original solution was found here AppleScript: How to repeat a search for a file until it is found?
I'm having trouble reconciling these cases. 3 and 4 are exactly the same except for the application. Same for 5 and 6. I also substituted safari with brave. They seem to work the same. textedit also seems to work like the others. Is finder just a special case or is there something else going on?
# Case 1) Give tell a string, get last window index: succeeds.
tell application "finder"
get index of last window
end tell
# Case 2) Give tell a variable, get last window index: succeeds.
set the_application to "finder"
function_a(the_application)
on function_a(the_application)
tell application the_application
get index of last window
end tell
end function_a
# Case 3) Give tell a variable, get next-to-last window index for finder: fails.
# 651:656: execution error: Finder got an error: Can’t get window before Finder window id 63457. (-1728)
set the_application to "finder"
function_b(the_application)
on function_b(the_application)
tell application the_application
get index of window before last window
end tell
end function_b
# Case 4) Give tell a variable, get next-to-last window index for safari: succeeds.
set the_application to "safari"
function_c(the_application)
on function_c(the_application)
tell application the_application
get index of window before last window
end tell
end function_c
# Case 5) Give tell a string, get next-to-last window index: succeeds.
tell application "safari"
get index of window before last window
end tell
# Case 6) Give tell a string, get next-to-last window index: fails.
1420:1425: execution error: Finder got an error: Can’t get window before Finder window id 63457. (-1728)
tell application "finder"
get index of window before last window
end tell
# Case 7) Give tell a string, get next-to-last finder window index: succeeds.
tell application "finder"
get index of finder window before last window
end tell
Any scriptable application has an unique AppleScript dictionary.
Of course applications can have something in common like windows and documents but the implementation of the windows element and the treatment of indices are individual. There is no general convention how to do that. Even properties with the same name can have different four-character-codes (the internal terminology identifier) in different applications.
For example the NSPositionalSpecifier must be implemented explicitly in the target application to be able to use the before/after syntax.
In my option the effort to write reusable scripts for multiple applications is a waste of time.
I will show you the solution for the Safari as example.
Case ONE (probably, it is your case): you Safari's settings is "open new windows as windows":
set the_application to "Safari"
function_c(the_application)
on function_c(the_application)
run script "tell application \"" & the_application & "\"
get index of window before last window
end tell"
end function_c
Case TWO (if you Safari's settings is "open new windows as tabs"):
set the_application to "Safari"
function_c(the_application)
on function_c(the_application)
run script "tell application \"" & the_application & "\"
get index of tab before last tab of window 1
end tell"
end function_c
The same as the code above, but for the multiple applications:
set the_application to choose from list {"Safari", "Finder", "TextEdit"}
if the_application is false then return
set the_application to item 1 of the_application
function_c(the_application)
on function_c(the_application)
run script "tell application \"" & the_application & "\"
get index of last window
end tell"
end function_c
Other beautiful example:
return {|Safari|:function_c("Safari"), |Finder|:function_c("Finder"), |TextEdit|:function_c("TextEdit")}
on function_c(the_application)
try
run script "tell application \"" & the_application & "\"
get index of last window
end tell"
on error
return missing value
end try
end function_c
I am trying to make an applescript that submit a form on mxtoolbox.com that uses AJAX at this link:
http://mxtoolbox.com/EmailHeaders.aspx
tell application "Safari"
activate
tell (make new document) to set URL to "http://mxtoolbox.com/EmailHeaders.aspx"
delay 10
tell application "System Events"
-- Copies text from variable headerText to clipboard and paste. Faster than keystroke headerText
set the clipboard to headerText
keystroke "v" using command down
end tell
end tell
Any ideas?
A suggestion was the following but it still did not work:
tell application "Safari"
activate
tell (make new document) to set URL to "http://mxtoolbox.com/EmailHeaders.aspx"
delay 10
tell application "System Events"
set the clipboard to headerText
keystroke "v" using command down
tell application "Safari"
do JavaScript ("
document.getElementById('ctl00_ContentPlaceHolder1_txtToolInput').value='" & headerText as text) & "';
document.getElementById('ctl00_ContentPlaceHolder1_btnAction').click();
" in document 1
end tell
end tell
end tell
You could do this with JavaScript:
set headerText to "test"
tell application "Safari"
activate
tell (make new document) to set URL to "http://mxtoolbox.com/EmailHeaders.aspx"
delay 5
do JavaScript "
document.getElementById('ctl00_ContentPlaceHolder1_txtToolInput').value=" & quoted form of headerText & ";
document.getElementById('ctl00_ContentPlaceHolder1_btnAction').click()" in document 1
end tell
So I have a Hazel rule that says "If I see a statement from this company, do X" in this case I want X to do the following.
Open Evernote
Check to see if I have a note with the same title as the downloaded file's title.
If no, create a note from that file. If yes, do nothing.
So what I wrote so far is.
global fileNamez
tell application "Finder"
set fileNamez to name of theFile
tell application "Evernote"
activate
delay (3)
set searchString to "\"" & fileNamez & "\""
set matches to find notes searchString
if (not (matches exists)) then
display dialog "no matches"
create note title fileNamez from file theFile
end if
end tell
end tell
The problem is the searching, it does not work, and I don't know whats wrong with it. Anyone have any ideas?
Try:
tell application "Finder" to set fileNamez to name of theFile
if application "Evernote" is not running then
launch application "Evernote"
delay 3
end if
tell application "Evernote" to set matches to find notes fileNamez
if matches = {} then
tell application "Evernote" to set resultNote to create note from file theFile title fileNamez
tell application "SystemUIServer" to display dialog "no matches" buttons {"OK"}
end if
How would I quit all running user applications using Applescript?
It's okay... I think I found my answer:
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder"}
try
tell application "Finder"
set process_list to the name of every process whose visible is true
end tell
repeat with i from 1 to (number of items in process_list)
set this_process to item i of the process_list
if this_process is not in white_list then
tell application this_process
quit
end tell
end if
end repeat
on error
tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
After some googling, I found a better approach:
It uses background only to build the initial app list, rather than
visible is true. The difference is that the other scripts will fail
to quit an app that's been hidden with ⌘H.
It provides an exclusions
list so that, for example, you can prevent your script editor from
quitting each time you test the script.
Adapted from a thread on MacScripter.
-- get list of open apps
tell application "System Events"
set allApps to displayed name of (every process whose background only is false) as list
end tell
-- leave some apps open
set exclusions to {"AppleScript Editor", "Automator", "Finder", "LaunchBar"}
-- quit each app
repeat with thisApp in allApps
set thisApp to thisApp as text
if thisApp is not in exclusions then
tell application thisApp to quit
end if
end repeat
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder"}
try
tell application "Finder"
set process_list to the name of every process whose visible is true
end tell
repeat with i from 1 to (number of items in process_list)
set this_process to item i of the process_list
if this_process is not in white_list then
tell application this_process
quit
end tell
end if
end repeat
on error
tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
tell application "System Events" to set quitapps to name of every application process whose visible is true and name is not "Finder"
repeat with closeall in quitapps
quit application closeall
end repeat