pass value as argument into handler - applescript

What is the proper way to pass list view as an argument into the function? If I set view_type inside the function it works, but if I set it outside the function, it does not.
#works
the_function()
on the_function()
tell application "finder"
set view_type to list view
set current view of window 1 to view_type
end tell
end the_function
#fails
set view_type to list view
the_function(view_type)
on the_function(view_type)
tell application "finder"
set current view of window 1 to view_type
end tell
end the_function

only the Finder knows what a "list view" is.
Try:
tell application "Finder" to set view_type to list view
the_function(view_type)
on the_function(view_type)
tell application "Finder"
set current view of window 1 to view_type
end tell
end the_function
you could also get tricky like:
the_function("lsvw" as constant) # lsvw, clvw, flvw, or icnv .
on the_function(view_type)
tell application "Finder"
set current view of window 1 to view_type
end tell
end the_function

Related

How to get the application name for a window reference

When I get the reference to a window using for example:
tell application "Safari"
set theWindow to first window
end tell
log(process(theWindow)) -- # Safari
tell application "Terminal"
set theWindow to first window
end tell
log(process(theWindow)) -- # Terminal
to process(theWindow)
-- How to get app name from "theWindow"
end process
From the variable theWindow, is it possible to extract the app name "Safari"? I need to know this information from a handler because the handler can accept window reference of other application as well.
tell application "Safari"
set theWindow to first window
end tell
log (process(theWindow))
tell application "Terminal"
set theWindow to first window
end tell
log (process(theWindow)) -- # Terminal
to process(theWindow)
try
theWindow as text
on error errorMessage
end try
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\""
set appName to text item 2 of errorMessage
set AppleScript's text item delimiters to ATID
return appName
end process

AppleScript to focus on a window in Preview app

Im trying to do some manipulation with preview using Automator. I have several windows open in preview, two of which are of interest. One is named "Markup Badges.png" one is called "Screenshot.png"
I want to set the focus to "Screenshot.png", copy the image and close the window, then I want to close the "Markup Badges.png" window.
I am having a lot of trouble getting this to work.
As part of my experimenting I have created two scripts trying to get the windows in focus, so I can then perform additional actions on them
Script 1:
on run {input, parameters}
tell application "Preview"
set visible of every window whose visible is true to false
end tel
tell application "Preview"
try
set theWindow to 1st window whose name begins with "Screenshot.png"
set index of theWindow to 1
activate
end try
end tell
-- stuff
delay 1
tell application "Preview"
set visible of every window whose visible is false to true
end tell
return input
end run
Script 2:
on run {input, parameters}
tell application "Preview"
set visible of every window whose visible is true to false
end tell
tell application "Preview"
try
set theWindow to 1st window whose name begins with "Markup Badges.png"
set index of theWindow to 1
activate
end try
end tell
-- stuff
delay 1
tell application "Preview"
set visible of every window whose visible is false to true
end tell
return input
end run
If I run Script 1 on its own, it does what I expect, Screenshot.png window is shown and has focus.
If I run Script 2 on its own, it does what I expect, Markup Badges.png window is shown and has focus.
If I run both scripts (play button in automator, Script 1 runs, followed by script 2) then script 2 does not work as it does on its own. The window is shown, but it does not have focus meaning I cannot send any keypresses to the window.
Any help appreciated
I found changing the code to the below fixed the problem.
on run {input, parameters}
tell application "Preview"
set visible of every window whose visible is true to false
end tell
tell application "Preview"
try
set theWindow to 1st window whose name begins with "Markup Badges.png"
set index of theWindow to 1
activate
tell application "System Events" to tell process "Preview" to perform action "AXRaise" of window 1
end try
end tell
-- stuff
delay 1
tell application "Preview"
set visible of every window whose visible is false to true
end tell
return input
end run

Applescript - What does "AXMain" mean?

In this script:
global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end tell
end tell
return {frontAppName, windowTitle}
What does "AXMain" mean? I cannot find the documentation or other questions asking about this anywhere!
AX is a class prefix which represents Accessible.
AXMain of a window means Is this window the main window of the application.

AppleScript get attribute won't return value when app is in fullscreen

This script works when terminal application(or any application) is not in FullScreen mode. It will return the correct value of false. When you make the application fullscreen it returns any empty value. The only thing I can think of is that when you go fullscreen it puts it on a different desktop? Am I missing something to activate the app differently now that it is fullscreen?
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder", "AppleScript Editor", "Google Chrome"}
tell application "System Events"
set process_list to the displayed name of every process whose visible is true
set process_number to (number of items in process_list)
set myList to process_list
end tell
repeat with theItem in myList
if theItem is not in white_list then
log theItem
tell application "System Events" to tell process theItem
set isFullScreen to the value of attribute "AXFullScreen" of windows
end tell
end if
end repeat
return theItem & " application is FullScreen: " & isFullScreen
This line won't work as written...
set isFullScreen to the value of attribute "AXFullScreen" of windows
"windows" will return a list of windows and you can't get that attribute from a list. So you would want to write it as...
set isFullScreen to the value of attribute "AXFullScreen" of window 1
However, with that said I tried this and it seems you can't get the windows from a full screen process. The list of windows is always empty {}. So this approach of determining if an application is full screen will not work.
You'll need to think of another way to figure out if an application is full screen. I tried a couple things and couldn't find a solution. Sorry.

Quit All Applications using Applescript?

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

Resources