Check if Private browsing menuitem is checked in safari applescript - macos

I want to disable the private browsing using apple script
before i run the disable code i want to make sure PrivateMode is enabled
for disabling i use following apple script
tell application "System Events"
tell process "Safari"
click menu item "Private Browsing" of menu 1 of menu bar item "Safari" of menu bar 1
end tell
end tell
The problem is that if i run this script and private browsing is not activated it will activate the private mode.
So i want the check to detect if private mode is enabled .So only then i will call the above apple script
Thanks

Use the "AXMenuItemMarkChar" attribute to get the checked of a menu item.
tell application "System Events"
tell process "Safari"
tell menu item "Private Browsing" of menu 1 of menu bar item "Safari" of menu bar 1
if value of attribute "AXMenuItemMarkChar" is "✓" then click -- disabling
end tell
end tell
end tell
Updated:
To not open an application (AppleScript applet or droplet) to the foreground : Use this script to add the property LSBackgroundOnly in the Info.plist of the bundle (the applet).
set tApp to choose file with prompt "Select your application (applet AppleScript)"
set tPlist to quoted form of ((POSIX path of tApp) & "Contents/Info")
do shell script "/usr/bin/defaults write " & tPlist & " LSBackgroundOnly -bool TRUE"
do shell script "/usr/bin/defaults write " & tPlist & " LSUIElement -bool TRUE"

Fun fact:
Running Safari on a German system, the menu item title "Private Browsing …" switches to "Private Browsing" (without the three points). It is impossible to switch private browsing on and off with the same handler
tell application "System Events"
tell process "Safari"
try
-- turn Private Browsing on
click menu item "Privates Surfen …" of menu 1 of menu bar item "Safari" of menu bar 1
-- turn Private Browsing off
click menu item "Privates Surfen" of menu 1 of menu bar item "Safari" of menu bar 1
end try
end tell
end tell
I don't have an English system here, maybe there is such title switch on your system, too?
Michael / Hamburg

Related

Applescript click 'share dropbox link' using contextual menu

I am trying to access the 'share dropbox link' item the contextual menu of a specific file in a specific dropbox folder using AXShowMenu.
I have found the following script. Via Applescript right click a file
tell application "System Events"
tell process "Finder"
set target_index to 1
set target to image target_index of group 1 of scroll area 1
tell target to perform action "AXShowMenu"
end tell
end tell
Which seem to do the trick for desktop items,
but how can I use this to target specific files in folders then click the 'share dropbox link' in the contextual menu?
A little kludgey, but it does work. This requires you have the dropbox folder open and frontmost in Finder.
tell application "Finder"
activate --brings Finder to front
select item 1 of window 1 --selects the first item in the list ; not sure how you want to select/determine the item
end tell
delay 0.5 --this is to insure you don't get unwanted typing in script editor (makes sure Finder activates before kludgey typing gets done)
tell application "System Events"
tell application process "Finder"
set _selection to value of attribute "AXFocusedUIElement" --focused element (selected element)
tell _selection to perform action "AXShowMenu" --contextual menu
end tell
keystroke "share dropbox link" --type to select contextual menu item
keystroke return --select it by hitting return
end tell
[EDIT] Here's a trick to make sure your dropbox folder is open and frontmost in the Finder:
tell application "Finder"
activate --brings Finder to front
tell me to set dbFolder to POSIX file ((do shell script "cd ~/Dropbox/;pwd") & "/")
open dbFolder
select item 1 of window 1 --selects the first item in the list ; not sure how you want to select/determine the item
end tell
tell application "System Events"
tell application process "Finder"
set _selection to value of attribute "AXFocusedUIElement" --focused element (selected element)
tell _selection to perform action "AXShowMenu" --contextual menu
end tell
keystroke "share dropbox link" --type to select contextual menu item
keystroke return --select it by hitting return
end tell
There may be another way of selecting the contextual menu, but this is what I came up with so far late at night after a glass of wine.

GUI automation with AppleScript - How to click a button in the script

I'm trying to write an AppleScript that will use the Save as Pictures... function of PowerPoint, but I'm wrestling with AppleScript. This is what I have:
set p to "file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
delay 2
click menu item "Save as Pictures..." of menu bar item "File" of menu bar 1
end tell
and it isn't doing what I want. The specific error I get is:
script error: Expected end of line, etc. but found class name. (-2741)
And I don't know what to do. I've tried all sorts of things but I can't seem to get the right menu item to click. I'm using OSX 10.9 and PowerPoint 2011 (Version 14.3.2)
UPDATE:
This is now the script I have:
set p to "file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
end tell
delay 2
tell application "System Events"
tell process "PowerPoint"
click menu item "Save as Pictures..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell
And I'm getting an execution error: Microsoft PowerPoint got an error: Parameter error. (-50)
Gui automation is generally done through the "System Events" app.
Also you need to provide a full path to a file before trying to open it.
This is how you would begin your attack on PowerPoint correctly.
set p to POSIX file "/Users/drew/Desktop/file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
end tell
delay 2
tell application "System Events"
tell process "PowerPoint"
click menu item "Save as Pictures..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell

How to easily enable and disable Java in Safari

My wife needs to have Java enabled to enter her timecard into ADP (grrrr), but can never remember how to do it using the Preferences pane. Is there a script I can use to do this?
I found two scripts that when merged let me create two Automator services for her: the core applescript and then how to detect if the checkbox is already on or off
on run {input, parameters} -- use for both Automator services except for false/true below
tell application "System Events"
tell process "Safari"
click menu item "Preferences…" of menu "Safari" of menu bar item "Safari" of menu bar 1
click button "Security" of tool bar 1 of window 1
set theCheckbox to checkbox "Enable Java" of group 1 of group 1 of window "Security"
tell theCheckbox
set checkboxStatus to value of theCheckbox as boolean
if checkboxStatus is false then click theCheckbox
-- false for the 'Enable' script, true for the 'Disable' one
end tell
click button 1 of window "Security"
end tell
end tell
return input
end run

Applescript running from AppleScript Editor not from the menu using BBEdit

I've read here around but I didn't find any fix for this stupid problem.
BBEdit, the most famous Mac text editor, should be widely scriptable and actually so it is. But,.. using Applescript I was trying to execute a menu command and there is no way at all. Or better, if I alternatively try:
tell application "System Events"
tell process "BBEdit"
tell menu bar 1
tell menu bar item "Markup"
tell menu "Markup"
tell menu item "CSS"
tell menu "CSS"
click menu item "Format"
end tell
end tell
end tell
end tell
end tell
end tell
end tell
Or also:
tell application "System Events" to keystroke "+" using {command down, shift down}
They works both running the Script from the Editor, but they don't work once I save the script and I choose it from BBEdit's AS Menu. Any idea ? Thanks.
Check how your script is saved: BBEdit's Script menu only runs compiled Applescripts (.scpt files), not text Applescripts (.applescript).
BTW, instead of the 7-way nested tell in the first snippet, just one nest suffices:
tell application "System Events"
tell process "BBEdit"'s menu bar 1's menu bar item "Markup"'s menu "Markup"'s ¬
menu item "CSS"'s menu "CSS" to click menu item "Format"
end tell

Script for adding new KeyboardShortcuts on MacOs (Leopard)

Is it possible to add KeyboardShortcuts in MacOs (Leopard) using shell or other programmatic way? Basically, something to automate the steps of opening Keyboard&Mouse in SystemPreferences, selecting the last tab "KeyboardShortcuts", Clicking "+" to add a new one and filling the info.
Thank you
The following AppleScript should do the trick, with 3 variables:
app_name: name of an application that you want to assign the shortcut to, e.g. Safari
menu_title: exact menu name to execute
keystrokes: the actual shortcut
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
tell window "Keyboard"
click button 3 of tab group 1
tell sheet 1
click pop up button 1
click last menu item of menu 1 of pop up button 1
keystroke "/Applications/" & app_name & ".app"
keystroke return
keystroke return
delay 1
keystroke menu_title
keystroke tab
keystroke last item of keystokes using rest of reverse of keystokes
delay 1
click button "Add"
end tell
end tell
end tell
end tell
The code is referencing the following site:
http://www.rngtng.com/2010/10/29/applescript-to-create-keyboard-shortcuts/

Resources