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
Related
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
Using AutoHotkey, I have a very tiny script to write text:
^m::
SendInput Foo
Return
This works in most places like Notepad and chrome. However, it doesn't work for a particular program I am using. The program has a simple textbox which I can type text into. The program is called TextExpander. I am making sure the cursor is located in the textbox. For some reason, I can't get auto-hotkey to type text into it.
Any help? Tips?
You can look at ControlSetText. Use WindowSpy to check the control name. If it has one, you can use that to set the text.
Alternately, make sure you're trying SendInput/SendEvent/SendPlay.
If you have a way to reliably focus the box you're trying to enter text to, you can put your variable onto Clipboard and send paste via Ctrl+V SendInput, ^v.
When I copy a text from Microsoft Word and the text is a heading in the clipboard I see it included a paragraph number. Then in another application I must remove the number manually.
Can I copy just a text without any additional information?
(The same is with Chrome when you copy a URL it adds a http:// automatically)
Method 1:-
one way is right mouse click and use this paste special option
Method 2:-
assign a shortcut(Ctrl+Shift+V) for this operation (Note: by default Ms word not set shortcut for it so we need to set it by own)
File > Options > Customize Ribbon > Keyboard shortcuts: Customize.
on Left Categories List section, click on All Commands
Under right Commands List, select for PasteTextOnly
then Set the keyboard shortcut for PasteTextOnly as Ctrl+Shift+V
Method 3 permanent/Default Settings
use this option which will each time paste as plain text by default
Try this
Sub FormatFreeTextCopy()
Dim ffText As DataObject
Set ffText = New DataObject
ffText.setText Selection.Text
ffText.PutInClipboard
End Sub
Note: You would have to Reference to Microsoft Forms Object Library
There is a generic "paste plain text" solution using autohotkey open-source automation software:
^+v:: ; Text–only paste from ClipBoard
Clip0 = %ClipBoardAll%
ClipBoard = %ClipBoard% ; Convert to text
Send ^v ; For best compatibility: SendPlay
Sleep 50 ; Don't change clipboard while it is pasted! (Sleep > 0)
ClipBoard = %Clip0% ; Restore original ClipBoard
VarSetCapacity(Clip0, 0) ; Free memory
Return
Save this code to paste_plain_text.ahk script
Install autohotkey
Execute your script
It will paste plain text using Ctrl+Shift+V shortcut. You can put your script at Startup directory so it loads on Windows startup.
How can I mark a word in notepad++ and can easily insert a command, with the marked word as argument? E.g.: I have a text with the word "WORD1", I want to mark "WORD1" with the mouse, and after press any button/shortcut, or quick contextmenu with the right mouse-button, or anything like that, and it will make it to "\command{WORD1}".
Of course, I want to specify "\command", e.g. I can choose the needed command in a dropdown of the context menu, or choose a specific button/shortcut for any command.
Is that possible?
Try recording a macro doing what you want and then assigning a keyboard shortcut to it and from then on, you should be able to get it to do what you want.
Look at the Shortcut Mapper
Finding location of NP++ Macros
Type %appdata% in run and hit Enter, when the directory opens, locate Notepad++ and you see shortcuts.xml
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.