Applescript to toggle hyperlinks - ms-word

I'm trying to write an Applescript to toggle the hyperlinks in Microsoft word. (This is normally done by hitting Alt+F9).
Here's my script, but it doesn't work:
tell application "Microsoft Word"
keystroke F9 using {option down}
end tell
That just gives me an error:
"Expected end of line but found identifier"
If I use:
tell application "System Events"
tell application "Microsoft Word"
keystroke F9 using {option down}
end tell
end tell
it works but does nothing.
And if I use:
tell application "System Events"
tell application "Microsoft Word"
keystroke "Hello"
end tell
end tell
It just prints "Hello" in the applescript window. I need it to affect MS word.

No need to script simulated keyboard shortcuts here. To toggle field codes (e.g. between the form {HYPERLINK "http://www.stackoverflow.com"} and actual hyperlinks), use this script:
# toggle field codes
# same as option + F9
# tested in Microsoft® Word 2008 for Mac v 12.2.3
tell application "Microsoft Word"
set theView to active window's active pane's view
if (show field codes of theView) then
set show field codes of theView to false
else
set show field codes of theView to true
end if
end tell
Note that this will also switch other field codes, such as page numbers, off and on.

Related

WinActivate does not work as expected. Re-activating focus to the starting window

I am having some serious struggles fully grasping the control on activating windows and forcing their focus and foremost position.
In order to debug a larger script I made a separate script to test the use of WinActivate and again I am observing frustrating behaviour as it either all together ignores the title I have defined or is failing in some other way. In the smaller test script I am simply requesting that the window in which the hotkey was triggered be set as active after another action, specifically an input box
Below is the simple code for testing:
F10::
SetTitleMatchMode, 1
DetectHiddenWindows, Off
WinGetTitle, startTitle, A
msgbox % "Start Title = <" . startTitle . ">"
;WinActivate, startTitle
inputbox, mode, Test box, Testing,,260,160
sleep 500
WinActivate, startTitle
Return
This code does not properly activate the starting window. For example I execute the hotkey in an empty notepad window and upon submitting blank into the input box the focus becomes notepad++ on my second monitor. The second time I press the hotkey from within notepad (or another application) notepad does not lose focus. In a third execution I begin from notepad again and after the input box appears I switch the focus to another window. I again submit blank to the inputbox but that new window remains the focus and notepad is not activated or brought to the foremost position.
Can someone please explain to me what is going on with WinActivate?
I was having similar frustration with unexpected results making a windows script host file and I think I must be missing some fundamental detail in windows.
You are trying to activate a window that start with the literal text "startTitle".
You forgot(?) to either enter expression syntax with % or use the legacy way of referring to a variable %startTitle% (please don't use legacy).
Extra stuff:
You shouldn't specify SetTitleMatchMode and DetectHiddenWindows inside your hotkey statement. There is no need (unless there actually is) to set those every time you hit the hotkey. Just specify them at the top of your script once.
Both of them are useless for you though, below I'll show why. Also DetectHiddenWindows is already off by default.
WinGetTitle is not good to use for this. What you actually want to do is get the hwnd of the window you wish by using e.g. WinExist().
And then refer to the window by its hwnd. Much better than working with window titles, and impossible to match the wrong window as well. To refer to a window by its hwnd, you specify ahk_id followed by the hwnd on a WinTitle parameter.
And lastly, the concatenation operator . is redundant. Of course you may prefer to use it, but in case you didn't know, it can just be left out.
Here's your revised code:
F10::
_HWND := WinExist("A")
MsgBox, % "Start hwnd = <" _HWND ">"
InputBox, mode, Test box, Testing,,260,160
Sleep, 500
WinActivate, % "ahk_id " _HWND
Return

AppleScript adds Enter/NewLine to clipboard when paste into Word

I'm currently trying to write an AppleScript for my Touchbar, which does copy a predefined variable into the clipboard and then pastes it into a word document.
Well, it does work, but for some reason in addition of just pasting the variable text, it also adds a Enter/NewLine to it.
Now I already did some testing and just assigned plain text directly to the clipboard and the NewLine was still there, so it isn't the variable. I also tested the Cmd+V output and also when I just paste the text manually afterwards it still pastes it with an enter, so it seems like there is something wrong with the assignment. But since I'm new to AppleScript and the language is really weird, I don't really get what I'm doing wrong here. Here is the important code part:
activate application "Microsoft Word" --I'm doing this, but you prolly don't need to.
set Donnerstag12 to "Anwendungsentwicklung"
set the clipboard to Donnerstag12
tell application "System Events" to keystroke "v" using {command down}
Now I just noticed that when I let AppleScript paste into Pages, TextEdit or straight into the code, it doesn't include a NewLine. Any Ideas?
You can use the type text command of Microsoft Word.
set Donnerstag12 to "Anwendungsentwicklung"
tell application "Microsoft Word" to type text selection text Donnerstag12
It must be something that Word is doing with it's "Smart Cut & Paste" options, though I'm not seeing that behavior in Word 2011. You can look to alter that behavior in the advanced Advanced settings. You can also just alter your script to delete the paragraph marker when you're pasting into Word.
activate application "Microsoft Word" --I'm doing this, but you prolly don't need to.
set Donnerstag12 to "Anwendungsentwicklung"
set the clipboard to Donnerstag12
tell application "System Events"
keystroke "v" using {command down}
if name of (first process whose frontmost is true) is "Microsoft Word" then key code 51
end tell

Trying to get the contents of an email message in AppleScript without added characters

Okay, I need to add some emails to my Daylite tasks automatically based on certain criteria, so I set up a Mail.app rule and an AppleScript to accomplish this. There's only one problem: when the AppleScript gets the content of the message, it adds an 'a' at the end (and makes a beep as well, letting me know it tried some keystroke that apparently wasn't allowed).
Here's what the AppleScript looks like:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
try
set this_subject to subject of eachMessage
set this_body to content of eachMessage
if this_subject is "" then error
on error
set this_subject to "No subject."
end try
activate application "Daylite"
tell application "System Events"
keystroke "t" using {command down, shift down}
keystroke this_subject
keystroke tab
keystroke this_body
keystroke "s" using command down
end tell
end repeat
end tell
end perform mail action with messages
end using terms from
This results in me getting a new task in Daylite, where the task title is the email Subject, and is displayed correctly, but the attached note, which is the email's body, is not displayed correctly.
So, if I receive an email that matches my rule, and the email is:
From: Hypothetical Wife
Subject: Don't forget to take out the trash!
Body: If you forget one more time, I'm filing for a divorce.
That ends up with a task that looks like this:
☐ Don't forget to take out the trash!
If you forget one more time, I'm filing for a divorce.a
… making my hypothetical wife in this scenario sound like a barely literate Canadian. (Plus there's an system alert sound, letting me know it apparently tried to type something that's not allowed.)
If the body has multiple lines of text, it also annoyingly removes the line-breaks.
I know I could set it to:
set the clipboard to this_body
and then replace
keystroke this_body
with
keystroke "v" using command down
…but that's a really inelegant solution and I don't want to replace whatever's in my clipboard every time the rule runs.
What am I doing wrong here? I've tested this in TextEdit as well, and the same problem occurs there too, so it's not a Daylite problem. I've also tried appending "as Unicode text" or "as string" or "as «class UTF8»" to the end of the "set this_body…" line, but none of those fixed the problem either.
Please, explain it to me like I'm a complete idiot because, as a complete idiot, that's the only way I'll understand you. Thanks.
One of the basic mistakes many people make is telling an application to do things that the application does not know how to do. You should only tell an application to do things that you can find in its applescript dictionary because that's really all the application knows how to do... besides some basic general purpose applescript stuff like performing a repeat loop.
You have fallen into this trap. You are telling Mail to tell Daylight and System Events to do things because all of that code is inside the "tell application Mail" block of code. This type of error often leads to problems that are very difficult to track down.
As such, my first suggestion would be to stop telling Mail to do unnecessary things. Here's how I would write your code to separate the different application commands from each other.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with eachMessage in theMessages
tell application "Mail"
set this_subject to subject of eachMessage
set this_body to content of eachMessage
end tell
if this_subject is "" then set this_subject to "No subject."
activate application "Daylite"
delay 0.2
tell application "System Events"
tell process "Daylite"
keystroke "t" using {command down, shift down}
delay 0.2
keystroke this_subject
delay 0.2
keystroke tab
delay 0.2
keystroke this_body
delay 0.2
keystroke "s" using command down
end tell
end tell
end repeat
end perform mail action with messages
end using terms from
You'll also notice I added a small delay between some of your code. Sometimes the code runs more quickly than the computer interface can handle, so that too may be a source of your error. You can also try increasing the length of the delay if needed (any delay over 1 second is overkill and shouldn't be necessary).
If these changes and a longer delay don't work then a simple solution would be to check if the text ends with "a" and just remove it. Something like this between the keystroke tab and keystroke this_body lines...
if this_body ends with "a" then set this_body to text 1 thru -2 of this_body
Good luck!
EDIT: the "keystroke" process doesn't seem to like the return characters from the email. I believe that's what causing the noise... when it hits a return character it beeps. Anyway, you can try something like this. Just open a blank TextEdit document and select an email in Mail and run the code in AppleScript Editor. You can incorporate this idea in your Daylight script.
tell application "Mail"
set eachMessage to item 1 of (get selection)
set this_subject to subject of eachMessage
set this_body to content of eachMessage
end tell
set bodyList to paragraphs of this_body
activate application "TextEdit"
tell application "System Events"
tell process "TextEdit"
repeat with i from 1 to count of bodyList
keystroke (item i of bodyList)
keystroke return
end repeat
end tell
end tell

Applescript: Close Word document? (Office 2011)

i'm simply trying to close a document (without quitting word), but this does not work.
I've tried :
close documents saving no
close active document
close document 1
but none of this is working..
The error I get (on the close line) is:
Microsoft Word got an error: The object you are trying to access does not exist
Here is a simplified script. Anyone have any ideas? All examples online seem to use this syntax, not sure if anything changed in the mac office 2011 version?
set input to {POSIX path of "/Users/Wesley/Desktop/test.doc"}
--ENABLE GUI SCRIPTING
tell application "System Events"
if UI elements enabled is false then set UI elements enabled to true
end tell
--RUN THE GUISCRIPT
set pdfSavePath to POSIX path of (choose folder with prompt "Set destination folder")
repeat with x in input
display dialog x
tell application "Microsoft Word"
activate
open x
set theActiveDoc to the active document
close theActiveDoc saving no
return
end tell
end repeat
close active document and close document 1 both work for me. Perhaps you need a delay statement before the theActiveDoc variable is assigned?

Form Fill Paste then Tab Repeat

I am trying to script some data entry using a applescript.
I need it to paste and then tab seven times and then paste again..... 2700 times.
While I am very new to programming, I was at least able to get something working.
What I have so far:
set the clipboard to "blah"
tell application "blah"
activate
tell application "System Events"
repeat 4 times
keystroke "v" using {command down}
keystroke " "
end repeat
end tell
end tell
Now this will paste into the first form and then skip one and paste into the next one. I dont know why. So this just pastes into every other form.
Any help?
Try:
set the clipboard to "blah"
tell application "blah"
activate
tell application "System Events"
repeat 4 times
keystroke "v" using {command down}
delay 1
repeat 7 times
keystroke tab
delay 1
end repeat
end repeat
end tell
end tell