3

This is for a .scpt file, triggered by a keyboard combo in FastScripts, entitled "Type As Single Line Plain Text."

The AppleScript code takes the clipboard text, removes all rich text formatting, and then removes all line breaks and indentations, setting this new string to a new variable, theClipboardTextWithoutAnyLineBreaksOrFormatting. Finally, I want the AppleScript to insert this new text at the blinking cursor.

But, I don't want to overwrite the original clipboard with this new, plain text string. I simply want the new string typed out at the blinking cursor, system-wide and in any application. keystroke does accomplish exactly what I want. It is just slow.

Here is the full script:

set theClipboardTextWithoutAnyFormatting to (the clipboard as text)

set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
set theClipboardTextWithoutAnyLineBreaksOrFormatting to text items of (theClipboardTextWithoutAnyFormatting as text)
set AppleScript's text item delimiters to {" "}

tell application "System Events" to keystroke theClipboardTextWithoutAnyLineBreaksOrFormatting

The problem with this method is that if theClipboardTextWithoutAnyLineBreaksOrFormatting contains a very long string, for example 2000 characters, then it will take a while for all of the text to be typed out, since it is being done manually in real time.

Is there a better way?

  • @user3439894 I've edited my question to include the context. – rubik's sphere Feb 11 '17 at 20:34
  • @user3439894 I've edited my question to include the rest of the code. – rubik's sphere Feb 12 '17 at 1:02
  • @user3439894 The "target file" can literally be anything and anywhere where text can be typed, from a comment field on a Stack Exchange question in Chrome.app, to renaming a folder in Finder.app, to the body of an .rtf file in TextEdit.app. If the text is to be typed into a file (which may not necessarily be the case, as explained in the previous sentence), then the file would not only have to be open, but also the frontmost window, and the blinking cursor would have to be set in the location where the user wants the text to be typed. My current code accomplishes all of this perfectly. – rubik's sphere Feb 12 '17 at 1:17
  • This is a completely different question to the original. Set your goalposts then leave them be. – Tetsujin Feb 12 '17 at 7:08
1

Yes.

User @jackjr300 has devised an ideal solution to this question here:

This solution works system-wide and does not affect the clipboard.

The provided TypeCharacters Objective-C file is significantly (i.e., exponentially) faster at typing text than AppleScript's native keystroke command.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.