Re: Xcode 4 editor scripting - help!
Re: Xcode 4 editor scripting - help!
- Subject: Re: Xcode 4 editor scripting - help!
- From: Tom Seddon <email@hidden>
- Date: Tue, 28 Feb 2012 01:09:06 +0000
Here's the best approach I've come up with. It works, if you hold your nose, but it's still terrible.
My goal: Insert "->" when Alt+, is pressed.
1. Use Apple Script Editor to create a .scpt file with the following contents. I called mine "~/bin/Insert Arrow.scpt".
---8<---
tell application "System Events"
delay 0.1
keystroke "-"
keystroke ">"
end tell
---8<---
2. Create a shell script somewhere that invokes your scpt file using osascript. I called mine "~/bin/Insert Arrow.sh", and made it executable (not sure how important that is though).
---8<---
#!/bin/sh
osascript ~/bin/Insert\ Arrow.scpt
---8<---
3. Create a new Behaviour in Xcode's Preferences dialog, Behavious section. Call it whatever you like. Its action should be "Run", and get it to run the script you created earlier. Double click the little Command icon in its entry to set up keyboard shortcut.
When actually invoking this script using the keyboard shortcut, you need to release any modifier keys quickly, so they don't affect the "keystroke" commands, which can't create fake unmodified keypresses. This is what the "delay 0.1" is supposed to help solve, but obviously it's only a delay, so if you don't release the modifier keys within 0.1 seconds you'll get the keypresses sent through with the modifiers still held. Not a big deal for my "->" macro, which might just produce incorrect chars, but I suspect that dangerous menu items could be invoked via their keyboard shortcuts - so watch out.
(And obviously, you'll always be waiting a tenth of a second for your text to appear.)
I filed a bug about the lack of a scripts menu in Xcode and it was closed as a duplicate of #8079239. Maybe if you add one yourself, it can also be closed as a duplicate of #8079239; perhaps more bug reports will get it fixed sooner.
--Tom
P.S. When I was looking into this stuff, I also found another method. Here's what I wrote at the time: "I also figured out that one can create an Xcode-specific service that runs a script that replaces the current selection, and have it do an echo -n. That does what I want too, but the choice of keyboard shortcuts is limited (e.g., M-, doesn't seem to work as a shortcut for a service), and the output takes so long to appear - probably at least 1/4 sec - that it hardly seems worth the bother for the shorter bits of text. "
So that might be an option for you too.
On 27 Feb 2012, at 20:14, Brad Oliver wrote:
> Hi all,
>
> I've spent the better part of an hour trying to figure out how to do a simple text scripting task in Xcode. Can someone paste a simple script that will do the following:
>
> For the current forefront text document, replace the current selection with the string "test"?
>
> I've got a bunch of macros I use that act upon the current text selection and perform various changes and substitutions. But so far I've yet to craft even a simple script that works with Xcode 4.
>
> If I can get the simple bones of an AppleScript that does this, then that's probably all I need to figure out the rest.
>
> --
> Brad Oliver
> email@hidden
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden