简体   繁体   中英

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?

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

You can use the type text command of Microsoft Word .

set Donnerstag12 to "Anwendungsentwicklung"
tell application "Microsoft Word" to type text selection text Donnerstag12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM