Use AppleScript to block a sender - outlook

I would like to block a spammer's address using AppleScript.
With the Inbox message selected, I execute this script:
tell application "System Events"
click menu item "Block Sender" in menu item "Junk Mail" in menu bar item "Message" in menu bar 1 of application process "Microsoft Outlook"
end tell
Which results in this error:
System Events got an error: Can’t get menu item "Junk Mail" of menu
bar item "Message" of menu bar 1 of application process "Microsoft
Outlook".
The Junk Mail menu has a submenu:
The AppleScript Editor has been granted access to control my computer:
What am I missing?

I can't really test it with Outlook, but you missed to tell the menus "Message" and "Junk Mail" of the menu items:
tell application "System Events"
click menu item ¬
"Block Sender" in menu ¬
"Junk Mail" in menu item ¬
"Junk Mail" in menu ¬
"Message" in menu bar item ¬
"Message" in menu bar 1 ¬
of application process "Microsoft Outlook"
end tell

Related

Applescript: Open preview "Export to pdf" error: Preview got an error: Can’t get keystroke " "

I would like to open all the pdfs in a folder with preview and export them to another folder using "Export to PDF".
This is my issue so far.
Error
Preview got an error: Can’t get keystroke "
".
Apple Script
tell application "Finder"
set fl to files of folder POSIX file "/Users/documents/PDFs" as alias list
end tell
repeat with f in fl
tell application "Preview"
open f
tell application "System Events" to tell process "Preview"
delay 0.2
click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1
end tell
keystroke return
end tell
end repeat
You are getting that error because keystroke return is a system events command and you have it within the "Preview tell block"
It looks as if your intention of keystroke return is to actually click the "Save" button, then this should work for you...
tell application "Preview"
activate
tell application "System Events" to tell process "Preview"
delay 0.2
click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1
delay 0.2
click button "Save" of sheet 1 of window 1
end tell
end tell

AppleScript in automator error ("The action "Run AppleScript" encountered an error ")

I wanted to find a way how to disable/enable hot corners via keyboard shortcut.
I found this apple script
property theSavedValues : {"Mission Control", "Desktop", "Dashboard", "Launchpad"} -- for example
tell application "System Preferences"
set current pane to pane id "com.apple.preference.expose"
tell application "System Events"
tell window "Mission Control" of process "System Preferences"
click button "Hot Corners…"
tell sheet 1
tell group 1
set theCurrentValues to value of pop up buttons
if theCurrentValues is {"-", "-", "-", "-"} then
repeat with i from 1 to 4
set thisValue to item i of theSavedValues
tell pop up button i
click
click menu item thisValue of menu 1
end tell
end repeat
else
copy theCurrentValues to theSavedValues
repeat with i from 1 to 4
tell pop up button i
click
click last menu item of menu 1
end tell
end repeat
end if
end tell
click button "OK"
end tell
end tell
end tell
quit
end tell
So I created an service in Automator with this apple script, attached it to keyboard shortcut, whenever I run this script in automator, it works but when i hit the shortcut, im getting "The action "Run AppleScript" encountered an error ".
Any ideas what can be done to fix this?
Thank you
Oh my god I feel so dumb, the only thing needed was to grant accessibility permission to app from which I'm using this service :).
You can do this by going to (Mac OS)Settings -> Security&Privacy -> Accessibility -> and finding the app from which you are running the service. (In my case it's Finder.)

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.

Check if Private browsing menuitem is checked in safari applescript

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

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

Resources