Apple Script: Resize and position Virtual Keyboard / KeyboardViewer - macos

If these are too many questions in 1, please tell me it and I edit the question and split it into 3? question.
I am currently writing some apple scripts and tried some things with it.
I want to:
open virtual keyboard (keyboardViewer)
resize it
position it
deactivate controls like close restore and minimize
My scripts are these:
App 1:
tell application "System Events"
if not (exists (process "KeyboardViewer")) then
do shell script "/usr/bin/keyboardViewer"
end if
end tell
App 2:
tell application "System Events"
if exists (process "KeyboardViewer") then
click (process "KeyboardViewer"'s window 1's buttons whose subrole is "AXCloseButton")
end if
end tell
I am using the keyboardViewer by https://github.com/nriley/keyboardViewer in /usr/bin/

Seems I can not script my application. Resizing and positioning works with some applications but not with this one. Also hiding the controls on the top left of the window in mac os x is not possible.

Related

AppleScript: Toggle Between Safari Windows?

If I have two safari windows open, just one tab in each, how do I get applescript to switch to either of the windows at will? Or in other words, toggle between them.
I tried, activate window 1 (or, the first window), and activate window 2 (or the second window), only ever activates the first window.
I tried open window 1 etc, open doesn't exist.
I tried using the system events, click menu bar 1 option, thinking maybe menu bar 2 was for window 2, didn't work.
I tried making do javascript on a specific tab show that page, couldn't get that to work.
Ultimately I did figure out I could use a keyboard shortcut, but I wanted to see if there was a more 'vanilla' applescript way.
If the windows are not full screen (in different spaces) just change the index of the window
tell application "Safari"
set index of window 2 to 1
end tell
If the windows are in different spaces you have to switch the spaces by executing the keystrokes with System Events. The default values on the US keyboard are ⌃←
tell application "System Events"
key code 123 using (control down)
end tell
and ⌃→
tell application "System Events"
key code 124 using (control down)
end tell
When I do just this
tell application "Safari"
set index of window 2 to 1
end tell
The new window that shows up is frozen. I fixed this by doing this
tell application "Safari"
set theWindows to windows
set win2 to item 2 of theWindows
tell win2
set visible to false
set visible to true
set index to 1
end tell
activate
end tell

AppleScript to block Safari pop-up windows

I am trying to automate the blocking of pop-up windows in Safari.
I have tried the following defaults write operation
defaults write com.apple.Safari WebKit2JavaScriptCanOpenWindowsAutomatically -boolean false
But this fails to do it. I have also tried setting it as true but even that didn't help. I am currently trying to find a way to do it using AppleScript.
This is what I have written so far -
CMD_ACTIVATE='tell application "Safari" to activate'
CMD_NEWTAB='tell application "System Events" to keystroke "," using {command down}'
osascript -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB"
This opens up the preferences but then I draw a blank. Anyone with any suggestions on how to proceed? Also I don't really need a solution to this only in AppleScript any other way to do it would also be helpful.
Note: I have been using Mac OS only for a week now, and am not that well versed in the nuances of this OS, so please be a bit descriptive when answering.
Thanks.
Blocking pop-up window is done via Safari / preferences / tab Security where you need to set the correct checkbox.
This preference seems to be stored in your library / preferences file com.safari.plist which contains the flag com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptCanOpenWindowsAutomatically with value Yes when pop-up are blocked.
However, it may not be the only place to be changed and in any cases, it is not officially documented, so that place can be changed by Apple any time. This is not recommended to use it.
Going back to Applescript, because Safari is quite poor in terms of applescript handling events, you are forced to go via GUI scripting. That’s what you’ve started, but keep in mind that if Apple changes the layout of Safari preference window, your script must be reviewed.
When going through GUI scripting (which, again, should only be when no other solution found) you must understand structure of GUI objects. Window contains button, check box, tool bar... in a hierarchy model. For instance the preference window in Safari contains bellow the tool bar, an object "group 1" with itself contains many objects depending of the tool bar current selection. Once you understand that concept, the script below, which does what you're looking for, will be easy to understand with many comments:
tell application "Safari" to activate
tell application "System Events"
keystroke "," using {command down}
delay 0.2 -- leave sometime to open window
tell window 1 of process "Safari"
click button 6 of toolbar 1 -- Security button is number 6
delay 0.2
-- check if check box not yet set and set it.
if (value of checkbox 5 of group 1 of group 1) = 0 then click checkbox 5 of group 1 of group 1
end tell
delay 0.2
click button 1 of window 1 of process "Safari" -- click on red / close button
end tell
I am running on Safari 10.0.3. If your version is different, the preference window may be different. Then the script must be adjusted: the Security tab button may not longer be the number 6 in your version,...

Applescript to open an application in full-screen mode?

I'm trying to program Alfred to open my Terminal, Sublime Text, and Chrome with a workflow.
I would like for my terminal to open normally as a window, but I've been trying to get Chrome and Sublime to open full screen.
I was able to get Chrome to open up in full screen mode with:
on alfred_script(q)
tell application "Google Chrome"
tell window 1 to enter presentation mode
end tell
end alfred_script
However, this did not translate to work with my Sublime Text.
What am I missing here?
Another way to do this assuming you have not changed the default keyboard shortcut for "Enter Full Screen" is simply to have System Events invoke that shortcut (⌃⌘F). As with the other approach I've seen to doing this (changing the value of AXFullScreen—see mklement0's answer here for a thorough discussion of this method), this requires making the relevant window active.
For instance, to toggle the full-screen state of the frontmost window in Safari, run:
tell application "Safari" to activate
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
As found here (i need an applescript to open safari in full screen an to hide the toolbar on mavericks). The make new document line prevents the can't get window 1 error by opening a new tab if one has not previously been opened.
tell application "Safari"
make new document
activate
delay 3
tell application "System Events" to tell process "Safari"
set value of attribute "AXFullScreen" of window 1 to true
end tell
end tell

AppleScript works in Script Editor but not as application

I am pretty new to programming, especially with AppleScript. I wrote a simple script for Valentine's Day to play a song from iTunes and then open a flash animation file in Safari. When I run the script in ScriptEditor, everything works as desired, but when I export as a standalone application, it fails at the command to enable full-screen mode. I am assuming it is an issue with System Events. To be clear, the application functions to the end, but at the keystroke command I hear an alert sound and the window remains as-is.
I am running Yosemite, and am fully updated.
Ideally, I would like to open the file in Google Chrome to utilize Presentation Mode, but I can't even get Chrome to open the file.
Thanks for any advice! Here is the code:
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
close every window
end tell
set volume output volume 75
tell application "iTunes"
set currentVolume to sound volume
if player state is playing then
stop
back track
end if
play track "The Promise"
set player position to 6
end tell
delay 4
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
end tell
I agree with Jerry Stratton's comment that it could be an accessibility issue. However it also could be that you are issuing the keystroke command before Safari is ready to accept it. If it's opening a file then it could be busy and miss the keystroke command.
Also, I would move the system events code outside the Safari code and also just tell system events, rather than the Safari process, to perform the keystroke command. Try this as the Safari and System Events parts.
NOTE: I can't get Chrome to open a file either.
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
end tell
tell application "Safari" to activate
delay 1
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
Most likely you’ll need to allow your standalone application to use System Events. At some point you needed to do that for Script Editor; you’ll need to do the same for your standalone app.
You’ll find the option in System Preferences under Security & Privacy, then Privacy, and then Accessibility. There’ll be a list of apps, and your app is probably listed there without a check for “Allow the apps below to control your computer.”
You may need to use the “+” button to add your app to the list.
I have verified that I can use this simple script to make Safari full-screen; it will work if the app is given permission under Accessibility, and it will silently fail if not.
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
This is Yosemite, Mac OS X 10.10; it may be different in other versions of Mac OS X.

Tell PDF Viewer/Adobe Reader to go to page on OS X

I am trying to make an integrated presentation for my students.
System: OS X Lion 10.7
For that I need to show a foreground window running a 3D presentation which is remotely controlled by a command line on another screen.
Besides it I would like to show a small set of PDF slides behind it.
I need some way of "remote controlling" the backgrounded PDF Viewer (Adobe Reader or PDFView) to accept simple commands like
Go to Page x (ideally)
Go to first page (home button)
Go to next page (arrow down, arrow right, page down or scroll down will all do)
I prefer to run the PDF in Adobe Reader fullscreen mode (allows me to background it and overlay another window on top, but PDFView could do too.
I have tried various Applescripts (run with osascript), but with no success:
tell application "System Events"
tell process "Adobe Reader"
tell window "starkdemo.pdf" to key code 125
end tell
end tell
sends key down to my terminal
tell application "/Applications/Adobe Reader.app"
tell active pane of active window
large scroll down 1
end tell
end tell
gives me an error 63:69: script error: Expected end of line but found class name. (-2741)
I have found that the free Viewer Skim has extensive AppleScript scripting http://sourceforge.net/apps/mediawiki/skim-app/index.php?title=AppleScript
It also have the fullscreen view that I like. So I will be using this in the future. Also the applescript command is just one line:
tell document "starkdemo.pdf" of application "Skim" to go to page 7
Try:
activate application "Adobe Reader"
tell application "System Events"
tell process "Adobe Reader"
click menu item "Full Screen Mode" of menu 1 of menu bar item "View" of menu bar 1
delay 2
key code 125
delay 2
keystroke space
delay 2
end tell
end tell
Or try this with Acrobat Pro
tell application "Adobe Acrobat Pro"
tell PDF Window 1
read page down
end tell
end tell
Or
tell application "Adobe Acrobat Pro"
tell PDF Window 1
goto page 3
end tell
end tell

Resources