How to change the Dashboard Preferences to "As Overlay" in applescript - applescript

I'm making an applescript which automates adding dashboard widgets to the desktop. To do this I need to set the Dashboard settings to "As Overlay" instead of "As Space" or "Off". This is in System Preferences > Mission Control. Also, how do I do stop the script until the return/enter key is pressed? This is what I have to far (without changing settings or the enter key):
set icon to (path to me as string) & "Contents:Resources:icon.icns"
on delay duration
set endTime to (current date) + duration
repeat while (current date) is less than endTime
tell AppleScript to delay duration
end repeat
end delay
delay 2
do shell script "defaults write com.apple.dashboard devmode YES"
delay 2
do shell script "killall Dock"
delay 3
display dialog "Click and hold to select a widget" with title "DeskWidget" buttons {"Cancel", "Choose a Widget"} default button 2 with icon file icon
delay 2
tell application "System Events"
key code 111
end tell
the return/enter key would be here, but instead I just have delay 2
tell application "System Events"
key code 111
end tell
delay 3
do shell script "killall Dock"
Thanks

To set the Dashboard settings to "As Overlay" (tested on El Capitan), use this :
do shell script "defaults write com.apple.dashboard 'dashboard-enabled-state' -int 3 && defaults write com.apple.dashboard 'enabled-state' -int 2 && sleep 2 && killall Dock"
To get the pressed keys: it's not possible with AppleScript, this should be possible in a Cocoa-Applet (AppleScriptObjC code)

Related

Getting several apps in fullscreen in a row via Applescript

So I am writing a script that would set up my working environment by opening and then getting several apps in fullscreen via a single command line. I am having trouble with the "getting the apps in fullscreen" part, and the script execution is very confusing to me.
My hardware is a MacBook Pro (15-inch, 2017) running on macOS Catalina 10.15.7.
Here is the part of the script that behaves erraticaly when executed:
script1.scpt:
tell application "Sublime Text"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
When executed while the app (here, Sublime Text) is not in fullscreen via the command osascript script1.scpt, the app is activated and goes fullscreen, but key code 123 using {control down} does not work, without producing any error message or sound.
When executed while the app is already in fullscreen via the command osascript script1.scpt, the app is activated, stays in fullscreen as intended but I get this error message:
execution error: System Events got an error: Can't get window 1 of process 1 whose frontmost = true. Invalid index. (-1719)
Also, key code 123 using {control down} does not work either.
When executed via a shell script calling script1.scpt while the app is not in fullscreen, the app behaves exactly as in case 1.
When executed via a shell script calling script1.scpt while the app is already in fullscreen, the app behaves exactly as it should and everything seems fine. But if I lengthen the script to get 2 apps in fullscreen in a row as in script2.scpt (see below), the 2nd app does stricly nothing, and I get the error message of case 2 again.
script2.scpt:
tell application "Sublime Text"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
tell application "Skim"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
I have been spending hours trying to understand what is going on without success, so any help or clue would be very welcome.
The only way I found to get things working is to separate the code in separate steps with 0.5sec pauses in-between them, like in the following shell script code block. But I would like to find a faster and "cleaner" solution to my problem.
osascript -e "tell application \"Sublime Text\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Skim\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Finder\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Terminal\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell"'
EDIT: For the moment being, I managed to get my script doing what I wanted it to do pretty reliably. Not completely satisfied though, because I still have to rely on fixed delays and this makes the execution a bit slow. This is what the code looks like:
in .bash_profile:
cd ~/<path>/ ;
osascript workspace_on.scpt
workspace_on.scpt:
tell app "System Events" to tell process "Terminal" to set the value of attribute "AXFullScreen" of window 1 to true
delay 0.6
tell app "Sublime Text" to activate
delay 0.6
tell app "System Events" to tell process "Sublime Text" to set the value of attribute "AXFullScreen" of window 1 to true
delay 0.6
do shell script "open -a skim ~/<path>/main.pdf"
delay 1
tell app "Skim" to set interaction mode of document 1 to full screen mode
delay 0.6
do shell script "open ~/<path>"
delay 1
tell app "System Events" to tell process "Finder" to set the value of attribute "AXFullScreen" of window "latex-template" to true
Just to copy-and-paste my most recent comment from above:
“Like you, I've spent a bit too much time figuring out this problem. It's quite a frustrating situation because I had a few light bulb moments of inspiration that, in theory, ought to have worked, and worked well, but ultimately didn't work at all. ¯_(ツ)_/¯ But in the process, I've at least written out a naive implementation that incorporates conditional checks and operates on each app one-by-one. It's not what I had aimed for in my head, but it works. See below.”
set Apps to {"TextEdit", "ManOpen", "Usenapp", "QuickTime Player"}
tell application id "com.apple.SystemEvents"
set dock to list 1 in the process named "Dock"
repeat with A in the Apps
tell my application named A to activate
set _P to (a reference to the process named A)
set _W to (a reference to _P's front window)
set _B to (a reference to the value of _W's ¬
attribute "AXFullScreenButton")
set _M to (a reference to (_P's menu bar 1's ¬
menu bar items's menu 1's menu items ¬
whose name = "Enter Full Screen"))
tell (a reference to (the dock's ¬
first UI element whose ¬
name = A)) to if ¬
exists then click
tell _W to repeat 60 times -- 60 x 0.5 = 30 seconds
delay 0.5
if it exists then
click _B
if (the value of attribute ¬
"AXFullScreen") ¬
then exit repeat
end if
end repeat
end repeat
end tell
System information:
AppleScript version: 2.8 system version: 12.6 ("Monterey")

how to save keystroke output in AppleScript variable?

I have written an AppleScript that starts my shell by typing in the commands in the terminal. But I want to test the shell and so I want to save the command result in a variable. This is my code:
#!/bin/bash
osascript <<EOF
tell application "System Events"
keystroke "cd /Users/uwe/documents/coding/c/pshell"
delay 0.5
keystroke return
delay 0.5
keystroke "make compile_and_run"
delay 1
keystroke return
delay 0.5
keystroke "bash /Users/uwe/documents/coding/c/pshell/tests/user_test.sh"
delay 0.5
keystroke return
key code 126
delay 0.5
set arrow_result to keystroke return // this is where I want to save the output
display dialog arrow_result // it says there is no variable called 'arrow_result'
end tell
EOF
If its possible I could also store the result in a bash variable but I don't think that that works
You can of course send keystroke commands and then get the text content of the terminal window (and then parse it) with the following command:
set arrow_result to value of text field 1 of scroll area 1 of splitter group 1 of window 1 of process "Terminal"
But why be so perverted. There is no need to use Terminal, System Events and GUI scripting to complete your task. Instead, use the most powerful tool in the AppleScript language, the do shell script command:
#!/bin/bash
osascript <<EOF
set arrow_result to(do shell script "cd /Users/uwe/documents/coding/c/pshell
make compile_and_run
bash /Users/uwe/documents/coding/c/pshell/tests/user_test.sh")
display dialog arrow_result
EOF
NOTE: I tested my suggestion in the Terminal with following code:
osascript <<EOF
set arrow_result to do shell script "echo \"Hello, World!\""
display dialog arrow_result
EOF

Is there any automation script to uncheck "Require Password" to never after sleep and turn off sleep in mac?

System Preference Settings
Manual Steps:
Open System Preferences
Click on "Security and Privacy"
Click on "General" tab
Uncheck "Require Password" option
Disable "Screen Lock"
Close "System Preferences"
The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Example AppleScript code
-- # Check to see if System Preferences is
-- # running and if yes, then close it.
-- #
-- # This is done so the script will not fail
-- # if it is running and a modal sheet is
-- # showing, hence the use of 'killall'
-- # as 'quit' fails when done so, if it is.
-- #
-- # This is also done to allow default behaviors
-- # to be predictable from a clean occurrence.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
-- # Make sure System Preferences is not running before
-- # opening it again. Otherwise there can be an issue
-- # when trying to reopen it while it's actually closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
-- # Open System Preferences to the General
-- # tab of the Security and Privacy pane.
tell application "System Preferences"
activate
reveal anchor "General" of ¬
pane id "com.apple.preference.security"
end tell
tell application "System Events"
tell application process "System Preferences"
tell its window 1
-- # Wait until the 'Require password' check box exists.
set i to 0
repeat until exists checkbox 1 of tab group 1
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
-- # If the target check box is unchecked then quit.
if value of checkbox 1 of tab group 1 = 0 then
tell application "System Preferences" to quit
return
end if
-- # Else click the target check box.
click checkbox 1 of tab group 1
-- # Wait until the 'Enter the password' sheet exists.
set i to 0
repeat until exists sheet 1
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
-- # You can uncomment the 'set value of text field 1' and set the password
-- # and uncomment 'click button "OK"' to have the rest fully automated, or
-- # leave them commented, enter your password, press enter and the rest is
-- # automated. There is also a way to retrieve your password from your
-- # Keychain, however, that's for you to research and implement.
tell sheet 1
-- set value of text field 1 to "PASSWORD"
-- click button "OK"
set i to 0
repeat until exists button "Turn Off Screen Lock"
delay 0.5
set i to i + 1
if i ≥ 30 then return
end repeat
click button "Turn Off Screen Lock"
end tell
end tell
end tell
end tell
tell application "System Preferences" to quit
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Increase timeout when prompting using osascript from bash script

I'm working on some bash scripting for use in JAMF in my environment. My scripting skills include: googling other peoples scripts, clipping out pieces of those and pasting them into my own script. Then eating the paste. I'm having a bit of a bizarre issue with a piece of AppleScript that runs within a bash script. Original script follows:
#!/bin/sh
# Remove pre-existing settings
rm -rfv /var/db/scrubbed/hostName
rm -rfv /var/db/scrubbed/imageTech
rm -rfv /var/db/scrubbed/adBinding
# Prompt for Hostname of new computer
hostName="$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1' -e 'text returned of result')"
/bin/echo "Computer hostname set to $hostName"
This code listed works just fine. Pops up my dialog box as expected. Unfortunately the box times out after 60 seconds. This is part of an imaging process so if the tech walks away for a few minutes the script continues and the hostname does not get set. Doing some research I found the with timeout of X command in AppleScript. When I update the first block of code to this:
# Prompt for Hostname of new computer
hostName="$(/usr/bin/osascript -e 'Tell application "System Events" with timeout of 86400 to display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1' -e 'text returned of result')"
/bin/echo "Computer hostname set to $hostName"
If it helps, this runs in the actual apple script app
tell application "System Events"
with timeout of 86400 seconds
display dialog "Please enter the hostname of the new computer" default answer "" with title "Hostname" buttons {"Ok"} default button 1
end timeout
end tell
I'm a scrub! Send help!
You need line breaks in the script, just as in the regular AppleScript. You're also missing end timeout, end tell, and the seconds units.
hostName="$(/usr/bin/osascript -e 'tell application "System Events"
with timeout of 86400 seconds
display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1
end timeout
end tell' -e 'text returned of result')"
If all you were looking to achieve was getting the actual host name of the computer… this one line of AppleScript code will achieve that for you.
set hostName to host name of (system info)
Otherwise
First, the display dialog command is not handled by System Events. The display dialog command is a Standard Additions command.
Another thing to point out is while using the with timeout clause, the script will continue running after the specified amount of time which was set in the clause. This puts you right back to the initial problem of the script continuing, after the time out, without user input.
The display dialog command has a giving up after option which allows you to set the amount of seconds it will wait until the dialogue will close itself and continue on. You can then add an if… then… clause to stop the script if the display dialog gave up after the specified amount of time.
Here is an example of an AppleScript which may be more suitable to your needs. I added a “Cancel” button giving the user the option to stop the script. I also used 10 seconds for testing purposes rather than 86400…. Which can easily be edited
set enterHostName to display dialog ¬
"Please enter the hostname of the new computer" default answer "" with title ¬
"Hostname" buttons {"Cancel", "Ok"} default button 2 giving up after 10
if gave up of enterHostName then
return
else
set hostName to text returned of enterHostName
end if
Again, the code was only an example. You will have to tweak it to work correctly within Terminal. Or you can save the code as an AppleScript file and use the osascript command in terminal to run the AppleScript file.

Apple script open Terminal with ready command

I'm trying to open terminal using apple script with a ready command but without executing it and allowing user to do this just by clicking enter (so I don't want to use tell Terminal to do script)
One of the approaches I used is using keystrokes:
tell application "Terminal" do script "echo Hi!"
keystroke "abc"
end tell
but it doesn't work for me. Any ideas?
I think you want to start Terminal and have a command all lined up ready in the Terminal ready for the user so he/she only has to press "Enter". If so, you can do this:
tell application "Terminal"
activate
delay 1
tell application "System Events"
keystroke "echo hi"
end tell
end tell
Then the user just has to press Enter and the command echo hi will execute.
it is hard to understand what you mean.
You can't for instance have the terminal wait for a user to click its window.
(But you can poll for a keystroke after the terminal window is opened.)
You'd have to use a dialog before your code, in order to make the user enter the terminal consciously.
display dialog "Press ok to enter the terminal" buttons {"Cancel","Enter"} cancel button 1 default button 2
Other than that, the way you'd need to use system events to send keystroke to the Terminal
tell application "System Events"
tell application process "Terminal"
keystroke "abcd"
end tell
end tell
You can poll for a keypress in the do script command to your terminal with this:
read -n 1 -s MYCHAR </dev/tty
This will force the user to press enter from a do script
a=`read`

Resources