I want to get the selected text in the active document in Word 2011 from Apple-Script.
Thanks
Try:
tell application "Microsoft Word" to get selection's content
I've tried this, and it works
tell application "Microsoft Word"
activate
try
set selectedText to content of text object of selection
display dialog selectedText buttons {"OK"}
on error
display dialog "erreur" buttons {"OK"}
end try
end tell
Related
I would like to open a new tab on Safari from a display dialog box with a dynamic URL
Basically only the last digits of the URL are changing, the end has to come from the user
## dialogue box##
set theResponse to display dialog "Show me the ID" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
## Open URL##
set myURL to "https://myURL/"
on open location myURL
set URL to myURL & theResponse
tell application "Safari" to open URL
end open location
The box works perfectly and I see in the Script Editor that the results is taken into account:
tell application "Script Editor"
display dialog "Show me the ID" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
{button returned:"Continue", text returned:"123456"}
I'm just not sure how to open the URL from 2 different sources and what format I should use
If there are only a couple of URLs you could just use buttons, for example `{"Cancel", "Continue with X", "Continue with Y"}, and use different URLs based on the button and text returned.
on run
set response to (display dialog "Show me the ID" default answer "" with icon note buttons {"Cancel", "Continue with X", "Continue with Y"} default button "Continue with Y")
if button returned of response is "Continue with X" then
set baseURL to "https://URLx/"
else -- Continue with Y
set baseURL to "https://URLy/"
end if
openNewTab(baseURL & text returned of response)
end run
on openNewTab(myURL)
tell application "Safari"
activate
tell window 1 to set current tab to (make new tab with properties {URL:myURL})
end tell
end openNewTab
i would like to know if it's possible to change the look of Dialog Window in Applescript, for example change the icon (note, stop, caution) by a custom picture or icon instead.
And if it is possible to change the typography (alignment, font, bold ... etc.)
If yes, could you help me with an example for my code or provided a link to a good tutorial please!
set folderName to text returned of (display dialog "Please enter new folder name:" default answer "Folder_Name")
set loc to choose folder "Choose Parent Folder Location"
try
tell application "Finder"
set newFolder to make new folder at loc with properties {name:folderName}
end tell
display dialog "Successfully! Want to reveal the new folder?" with icon note buttons {"Cancel", "Go to my new folder"} default button "Go to my new folder" cancel button "Cancel"
if button returned of the result = "Go to my new folder" then
tell application "Finder"
reveal newFolder
activate
end tell
end if
end try
You can't change the typography. However you can use:
display dialog "hi" with icon withIconFile
where withIconFile is an alias or file reference to a ‘.icns’ file
This is better for me !
display dialog "My custom icon " buttons {"Cancel", "Continue"} default button "Continue" with icon file "Path:to:my.icon.icns"
You can also do:
display dialog "Hello" buttons {"Cancel", "Continue"} with icon {"/Users/" & (do shell script "whoami") & "/path/to/picture.jpg"}
The do shell script part just makes it work, even if you send it to your friends, although I would then recommend downloading the image from the internet with curl and placing it in a folder. Sorry if it is a bit confusing
Ok, I am trying to make a script where you type something and a new finder window appears with what you typed in it. Similar to spotlight search, but in a script.
set theFind to text returned of (display dialog "What do you want to find?" default answer "" buttons {"Cancel", "Ok"} default button 2)
tell application "Finder"
reveal theFind
end tell
There’s an AppKit method that does exactly what (I think) you’re asking for: -[NSWorkspace showSearchResultsForQueryString:], which means you can use it using AppleScriptObjC. So, in AppleScript Editor, File > New from Template > Cocoa-AppleScript Applet, then:
property NSWorkspace : class "NSWorkspace"
NSWorkspace's sharedWorkspace()'s showSearchResultsForQueryString_(theFind)
Alternatively, you could skip AppleScriptObjC and use the hidden Finder command that that method uses:
tell application "Finder" to «event aevtspot» theFind
I can create a new contact and open it for display in Mac Outlook 2011 using AppleScript:
tell application "Microsoft Outlook"
set newContact to make new contact with properties {first name:"Fred", last name:"Flintstone"}
open newContact
end tell
But this contact is already saved. Is there a way I can open a new and unsaved Outlook contact, fill in the properties, and allow the user to decide whether or not to save it?
I've tinkered around with "make new window" but I can't get anywhere there. I consistently get the error:
error "Microsoft Outlook got an error: AppleEvent handler failed." number -10000
I think I need to go about this a different way, but nothing in the Outlook AppleScript dictionary looks promising.
You can do this by scripting the UI elements to open a new contact:
tell application "System Events"
click menu item "Contact" of menu 1 of menu item "New" of menu 1 of menu bar item "File" of menu bar 1 of application process "Outlook"
end tell
Update:
But the caveat is that the new contact is not a AppleScriptable object until it is added to the Outlook database, i.e. it is saved. If you add these lines to the above script, you can see this:
tell application "Microsoft Outlook"
set contactWindow to item 1 of (windows whose index is 1)
get object of contactWindow
end tell
object of contactWindow is a missing value.
So if you want to use the Outlook Applescript dictionary APIs to edit the fields of a new contact, then that contact must have first been saved.
2nd Update:
The following, when placed in the 'tell application "System Events"' block after creating a contact will set the lastname, firstname and email address for that contact using UI element scripting:
set lastName to "Einstein"
set firstName to "Albert"
set emailAddress to "a.einstein#relativity.com"
set value of text field 1 of splitter group 1 of window 1 of application process "Outlook" to lastName
set value of text field 2 of splitter group 1 of window 1 of application process "Outlook" to firstName
set value of text field 6 of scroll area 1 of window 1 of application process "Outlook" to emailAddress
As mentioned in the comments, "Enable access for assistive devices" needs to be enabled for this to work. This can also be done programmatically from AppleScript:
-- turn on UI automation - may throw a permissions dialog
if UI elements enabled is false then
set UI elements enabled to true
end if
I've written a script that grabs information from Active Directory and creates a new Signature in Microsoft Outlook for Mac.
I use the following code to create the signature(I will leave the other code out, as it isn't really relevant):
tell application "Microsoft Outlook"
make new signature with properties {name:strName, content:contentHTML, plain text content:"", include in random:false}
end tell
Where strName is the name of the signature I get from elsewhere and contentHTML is the actual signature in HTML that I build elsewhere.
Adding this signature to Microsoft Outlook is working perfectly, but I can't see how to set the signature that I created to the default signature for the current account. I have done quite a lot of research that hasn't helped at all, and I've poked around the dictionary as well.
This can be done with AppleScript. There is nothing in the Outlook 2011 dictionary to specifically do this, so instead this can be done by scripting the UI elements, which admittedly is rather clunky.
Signatures are set on a per-account basis, so you need to provide the name of the account to this script as well as the name of the signature you want to set for that account.
setDefaultSignature to "strName" for "Gmail"
on setDefaultSignature to mySignature for accountName
tell application "Microsoft Outlook" to activate
tell application "System Events"
-- turn on UI automation - may throw a permissions dialog
if UI elements enabled is false then set UI elements enabled to true
click menu item "Preferences..." of menu 1 of menu bar item "Outlook" of menu bar 1 of application process "Outlook"
click item 1 of (buttons of window "Outlook Preferences" of application process "Outlook" whose description is "Signatures")
click button "Default Signatures..." of window "Signatures" of application process "Outlook"
repeat with thisRow in rows of table 1 of scroll area 1 of sheet 1 of window "Signatures" of application process "Outlook"
if value of text field of thisRow as string is accountName then
click pop up button 1 of thisRow
click menu item mySignature of menu 1 of pop up button 1 of thisRow
click button "OK" of sheet 1 of window "Signatures" of application process "Outlook"
click item 1 of (buttons of window "Signatures" of application process "Outlook" whose description is "close button")
exit repeat
end if
end repeat
end tell
end setDefaultSignature