AppleScript: Typing text/backing up the clipboard

Discussion in 'Mac Programming' started by BlueRevolution, Aug 27, 2009.

  1. BlueRevolution macrumors 603

    BlueRevolution

    Joined:
    Jul 26, 2004
    Location:
    Montreal, QC
    #1
    Okay, I'm tired of the 12 smileys here, so I wrote an AppleScript to make it easier to use Simple Smileys using [​IMG]. Easy-peasy.

    The problem is that in order to get the text into Safari, I've had to use the clipboard. This clears whatever I had on the clipboard before, which might be pretty inconvenient for posting pertinent URLs and such. This is the offending bit of code:

    Code:
    --set temp_clipboard to the clipboard
    set the clipboard to bbcode
    tell application "Safari" to activate
    tell application "System Events" to keystroke "v" using {command down}
    --set the clipboard to temp_clipboard
    When I uncomment the first and last lines, the script outputs whatever had been on the clipboard before it was run, and the contents of the clipboard don't change. I can't for the life of me work out why this is. It's like it ignores the second line altogether.

    Any suggestions? Using the clipboard is itself a workaround, and I'd prefer if my script could type the text directly. I haven't been able to figure out how to do that.

    Edit 1: The attached app isn't protected from import, so it can be opened in Script Editor.

    Edit 2 to add:

    I just tried using

    Code:
    tell application "System Events" to keystroke bbcode
    and it does work, but it takes some time to type the URL out. Using the clipboard is considerably faster.
     

    Attached Files:

  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    I decommented those two lines and your script worked fine for me (Leopard 10.5.8/Safari 4.0.3). That is, I copied some text, ran your script, the clipboard was reset to the copied text.

    Shot in the dark: What if Safari handles the clipboard slightly differently than the OS?

    Maybe this would work:

    Code:
    tell application "Safari"
    	activate
    	set the clipboard to bbcode
    end tell
    mt
     
  3. BlueRevolution thread starter macrumors 603

    BlueRevolution

    Joined:
    Jul 26, 2004
    Location:
    Montreal, QC
    #3
    Aha! It works. I still have no idea why it works, but it does work. Thanks! [​IMG]
     

Share This Page