"Sort Lines in Selection" for Xcode 4 - xcode

I like Textmate's "Sort Lines in Selection" feature a lot. Is there something similar for Xcode 4? If not, what would be the best way to integrate such a feature (a plugin, Applescript, ...)?

You can do this with Automator:
Start Automator and select either:"Service" for macOS 10.7 or"New Document" followed by "Quick Action" for macOS 11
Find and drag "Run Shell Script" into the workflow panel
Select "Output replaces selected text"
Type sort -f into the "Run Shell Script" textfield
Save
Now you can sort lines in any textfield. Select some text and right-click or Control click and select the service you just created.

After 4 years Xcode still doesn't have this feature builtin, but now it supports extensions. So here you go: "xcsort" is an extension to
sort text in Xcode 8. It adds a command to sort lines in selection.

For macOS 10.14+ (Mojave) users who are looking at epatel's answer, Automator has renamed Services to Quick Action. I was lost for a moment until I figured it out.

How
In TextMate, open the bundle and see how they have implemented it ;)
Specifically, they have used sort -f for that command.
In Xcode
Xc4 doesn't offer external commands, but Xc3 did... what version are you using?
Here's an overview for Xc3's script system: http://www.mactech.com/articles/mactech/Vol.23/23.01/2301XCode/index.html
Xc4 allows you to run an external script via Behaviours, but you cannot pass or return text/selection.
AppleScript
You may be able to do it with AppleScript... every time I have tried to do anything nontrivial with AS + Xcode, it didn't work out very reliably (if at all). But that was with Xc3 - locating what you need may be easier with Xc4 (unified UI and all).
I just use TextMate for this.

There's a bug on 10.7 and 10.8 where the shortcuts for Automator services don't always work until the services menu has been shown once from the menu bar. You can still select them from the context menu, but another option would be to assign a shortcut to a script like this:
try
set old to the clipboard as record
end try
try
tell application "System Events" to keystroke "c" using command down
do shell script "export LC_CTYPE=UTF-8; pbpaste | sort -fn | pbcopy"
tell application "System Events" to keystroke "v" using command down
delay 0.05
end try
try
set the clipboard to old
end try
Trying to get the clipboard when it's empty results in an error. pbpaste and pbcopy don't support Unicode by default in the environment used by do shell script.

Related

Automator Quick Action Only Runs in Automator and not in Finder Quick Action Menu

I have an Automator Quick Action (QA) which calls a bash script to act on files within a specified folder. It works great when run inside Automator. But invoking the Quick Action from Finder by right clicking on the input folder does nothing with no errors or log files (as far as I can tell). screenshot of Automator code at link. My first bash code, and first question on stack-overflow - thanks to anyone in advance who might be able to assist. Apple silicon mackbook pro; running Mac OS 12.4.
Additional Info: The QA gets a selected folder and passes it to the bash shell script via "$a" argument. The selected folder will contain text files (.SRT) and movie files (.MP4). The bash script does some text parsing and passes on arguments to the Exiftool program which adds GPS metadata to the movie files.
Workaround found: I was able to import the automator workflow into the Mac OS Shortcuts App and the automation ran successfully via a menu bar command on my Mac.
I had the same issue for a long time trying to invoke a python program from bash shell script using quick action. It runs with no error in the editor and would run only if I left the workflow open all of the time in Automator. I eventually stumbled across a permission issue with Automator itself. I hope this could help you maybe?:
1 - Go "System Preferences -> Security & Privacy -> Privacy"
2 - Select "Accessibility" from the left-hand menu.
3 - Unlock to make changes, if necessary, then click the "+" sign at the bottom of the right-hand menu.
4 - Add "Automator" as a trusted app.
Hope it helps!

Final Cut Pro X with Apple script (export projects)

Trying to make a script with whom will launch FCPX and will export one project after another (per day need to export 24 videos). So far were able to make this script:
tell application "Finder"
open ("/Volumes/UpNext/Final Cut Projects/1-Monday/CurrentVersion.fcpproject" as POSIX file)
end tell
delay 14
tell application "System Events"
tell process "Final Cut Pro"
click menu item "Snapping" of menu "View" of menu bar 1
click
end tell
end tell
Instead of turning on snapping I want to do following: Click in FCPX on /File/Share/Some format.
Problem is that don't know how to launch "share" and "needed format", because "Share" is an pop up menu. My apologies for simple questions, unfortunately never in my life worked with scripts and project needs to be done ASAP. Maybe there is other way with scripts to achieve this goal (export many projects). Will be open to suggestions!
I think the best (maybe easiest) way to do this (at least to get you started) is to set up key commands in System Preferences, and to use the following method to trigger the key command.
So for example, I just used System Prefs (Keyboard>Keyboard Shortcuts>Application Shortcuts) to make a Shift-F1 key command to trigger the "Vimeo..." menu item. Now I can use cliclick to trigger that. Cliclick (http://www.bluem.net/en/mac/cliclick/) is somewhat limited (well, more limited than the version I have on my machine :-) ), but it can come in handy. You just have to use an AppleScript do shell script command to use it:
activate application "Final Cut Pro Trial"
delay 0.5
do shell script "/your/path/to/cliclick kd:shift kp:f1 ku:shift"
The delay makes sure everything settles before the click (w/o the delay, click may fail).
(Obviously, replace the path in the example with a correct POSIX path -- mine is installed in /usr/local/bin/)

Xcode, default window layouts?

No-one has been able to answer this question.
It would seem to be impossible to have XCode open this way.
However Youssef provided the most useful answer - so Youssef gets the points, thank you Youssef.
Note for future readers .. the mac utility Moom is excellent for some, but not all, problems of this nature. Again it is not a total solution.
Using the current up-to-date XCode,
Whenever I OPEN a project in XCode (or start a new project),
it looks like this:
However, I want it to look like this:
Again that's when I OPEN a project.
Is there any way to achieve this? Thanks.
Go to Xcode (in the top menu bar) --> Preferences...
Switch to the Behaviors tab and modify what you want in the Running section
You'll get something like this:
You can choose to show/hide what you need, and specify the default behavior
Cheers
EDIT:
After understanding what you need, I will modify my answer.
There is no way to force XCode to automatically enter a behavior at startup.
The best you can do is to create a custom behavior and give it a keyboard shortcut. And when you start XCode just use the keyboard shortcut to enter the desired behavior.
This is how it is done:
In the behaviors tab (above) click on the + sign at the bottom
Then enter the desired name of this behavior (Maybe StartupBehavior)
Click on the left button to modify the shortcut key. My preferred shortcut key is Command + ` (the button to the left of the '1' key). This is because it will not override any other command and it is easy to click.
Note: you can specify any shortcut you like and dont be afraid to override one that already exists if you don't use it.
Now you need to configure your behavior the way you like it
Close the preference windows.
Now everytime you start XCode just use the shortcut key you specified (Command + ` in my case) to quickly load your behavior
Note:
If you dont like keyboard shortcuts you can also load your behavior by going to Xcode (menu bar) --> Behaviors --> Select your behavior
That is the quickest way you can achieve what you want. I dont think you will be able to force Xcode to run your behavior automatically on startup.
Hope it helped.
Cheers
(not really default layout, you have to hit at least 2 keys simultaneously once XCode is open) You could set up a behavior for "Build->Starts" and hit "CMD + B"
after opening XCode / any Project. This way it will 1. change to your
Layout even if there are any errors and Build fails and 2. you don't
have to Stop it again.
As already said, it's not a default layout, but I like to let the compiler run through projects of other people at the beginning anyway.
You could search for some Keys in XCodes preference file in
~/Library/Preferences/com.apple.Xcode.plist as already suggested in
my comment, I'm not experienced enough to know which are the right
ones, sorry.
Wrap the .xcodeproj File opening in an application. E.g. in no way "clean", but anyway:
Set up an XCode behavior like in option 1
Open Automator, choose Application, drag "open Finder Items" and "run AppleScript" inside.
on run {input, parameters}
set frontmostAppName to name of (info for (path to frontmost application))
repeat while frontmostAppName is not "XCode.app"
set frontmostAppName to name of (info for (path to frontmost application))
delay 0.5
end repeat
try
tell application "System Events" to keystroke "b" using {command down} --simulates CMD + B
end try
return input
end run
Insert some applescript like the above and save the Automator application.
RightClick -> getInfo on any .xcodeproj File in Finder/on Desktop, in "open with" select your new Automator app, click add and back in the info panel -> check "Change All"
You can obviously create a new Behavior instead of using Build->starts, assign a different Hotkey in XCode and script e.g. tell application "System Events" to keystroke "i" using {command down, control down, shift down, alt down} , optimize the applescript somehow or do everything via a real cocoa app if you want
Well, you may be able to approach it using preconfigured xcuserstate files nested in your project templates' default workspace directory, but that is going to be a pain to tweak and maintain. I'm not even sure it would work.
I would personally just approach it by using Behaviors.
So:
1) create a Behavior
2) Give it a convenient key binding
3) Then define the Behavior. For example:
ASCII Behavior Editor:
...
√ Show *Debugger* with *Console View*
√ *Hide* utilities
...
That should do it. Just hit the assigned key command when you want to restore that display state, regardless of its initial state.
Of course, you can do a few other things or all sorts of arbitrary stuff by running an external AppleScript (triggered with a Behavior's run script facility).

In OSX can I use a system call to pass a command to a running terminal in a new tab?

Specifically,
In OSX 10.6 from a system call, I want to open a file for editing with VIM in a preexisting terminal (i.e. Terminal.app) by opening a new tab.
Of course I can open a new instance of terminal
/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -e vim MyFile
And, of course I can figure out the PID of the running instance of Terminal but I don't know how to pass a command into that running program. Or, if Terminal supports receiving commands and if it will open a new tab.
If someone knows how to do this with a similar system (e.g. linux and xterm) it could help me figure it out with OSX and Terminal - or, is there some other technique to prevent opening so many terminals instances?
EDIT: CHEAP SOLUTION
I created an AppleAcript script
on run app_arg
tell application "System Events"
tell application process "Terminal"
key code {55, 36}
set frontmost to true
key code {55, 17}
keystroke item 1 of app_arg
keystroke return
end tell
end tell
end run
and run it via the system call like so
/usr/bin/osascript NEWSCRIPT.scpt "args"
It's dirty but it gets the job done - thanks!
The way to accomplish the is with applescript. You can send applescript to things in OS X with the osascript command. I wasn't able to find anything quickly that directly shows how to open a new tab with a command running in it, but I was able to find a couple of references to automating Terminal.app in various other ways with applescript, and I think they may point you in the right direction.
Various random Terminal.app applescript hacks mostly centered around changing colors.
An applescript hack that opens a new terminal window without creating a new Terminal process.
A nice StackOverflow question about how to query an application to discover what applescript it supports (stolen from a comment on your question by #dmckee)
And this nice StackOverflow question concerning Terminal.app's applescript specifically (again stolen from a comment by #dmckee)
An even better exploration of the Leopard Terminal.app's applescript from Ruby no less
And from that last link, it looks like the only way to do it is to use applescript to send the Command-T keystroke to the terminal. That's ugly, but it'll work. And then you can send the command you want to execute. :-)
There are three ways to do this:
Use popen
Use system
Use exec family

Open terminal here in Mac OS finder [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Closed 10 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
Is there something similar to the "Open Command Window Here" Windows Powertoy for Mac OS? I've found a couple plugins through a google search but wanted to see what works best for developers out there.
As of Mac OS X Lion 10.7, Terminal includes exactly this functionality as a Service. As with most Services, these are disabled by default, so you'll need to enable this to make it appear in the Services menu.
System Preferences > Keyboard > Shortcuts > Services
Enable New Terminal at Folder. There's also New Terminal Tab at Folder, which will create a tab in the frontmost Terminal window (if any, else it will create a new window). These Services work in all applications, not just Finder, and they operate on folders as well as absolute pathnames selected in text.
You can even assign command keys to them.
Services appear in the Services submenu of each application menu, and within the contextual menu (Control-Click or Right-Click on a folder or pathname).
The New Terminal at Folder service will become active when you select a folder in Finder. You cannot simply have the folder open and run the service "in place". Go back to the parent folder, select the relevant folder, then activate the service via the Services menu or context menu.
In addition, Lion Terminal will open a new terminal window if you drag a folder (or pathname) onto the Terminal application icon, and you can also drag to the tab bar of an existing window to create a new tab.
Finally, if you drag a folder or pathname onto a tab (in the tab bar) and the foreground process is the shell, it will automatically execute a "cd" command. (Dragging into the terminal view within the tab merely inserts the pathname on its own, as in older versions of Terminal.)
You can also do this from the command line or a shell script:
open -a Terminal /path/to/folder
This is the command-line equivalent of dragging a folder/pathname onto the Terminal application icon.
On a related note, Lion Terminal also has new Services for looking up man pages: Open man page in Terminal displays the selected man page topic in a new terminal window, and Search man Pages in Terminal performs "apropos" on the selected text. The former also understands man page references ("open(2)"), man page command line arguments ("2 open") and man page URLs ("x-man-page://2/open").
This:
https://github.com/jbtule/cdto#cd-to
It's a small app that you drag into the Finder toolbar, the icon fits in very nicely. It works with Terminal, xterm (under X11), iterm.
An application that I've found indispensible as an alternative is DTerm, which actually opens a mini terminal right in your application. Plus it works with just about everything out there - Finder, XCode, PhotoShop, etc.
Clarification (thanks #vgm64): if you're already in Terminal, this lets you quickly change to the topmost Finder window without leaving Terminal. This way, you can avoid using the mouse.
I've added the following to my .bash_profile so I can type cdff in Terminal at any time.
function ff { osascript -e 'tell application "Finder"'\
-e "if (${1-1} <= (count Finder windows)) then"\
-e "get POSIX path of (target of window ${1-1} as alias)"\
-e 'else' -e 'get POSIX path of (desktop as alias)'\
-e 'end if' -e 'end tell'; };\
function cdff { cd "`ff $#`"; };
This is from this macosxhints.com Terminal hint.
Check out Open Terminal Here. It may be the most similar to "Open Command Window Here." I used >cdto and this is very similar but this seems to be a little better at dealing with Spaces... but not perfect.
What it has that is very nice is the ability to "detect key-down events at the start of the application and used them to modify the behavior of the script" allowing the script to open a new tab in the front most terminal window when invoked by holding down ⌘ key. Neat trick.
Also note PCheese's answer; it is probably more useful for heavy terminal users!
There is an updated version of the very nice and slim Open Terminal Here posted by vgm64 and d0k. The change was made by james david low. He published the new version on his site. Just download OpenTerminalHere.zip, extract it, move the bundle to your Library/Scripts folder and drag it from there to your Finder toolbar.
What is special about it is that it always opens a new tab if a Terminal.app window is already open. Very useful! I also noted that the style of the button of the application better fits the Snow Leopard Finder.app style than cdto posted by redacted did.
Also, you can copy an item from the finder using command-C, jump into the Terminal (e.g. using Spotlight or QuickSilver) type 'cd ' and simply paste with command-v
I created a bundle with 3 apps for the finder toolbar.
The other two apps do:
open Textmate with the current selection
open GitX with the current folder
For more information see here:
http://nslog.de/posts/71
If you install Big Cat Scripts (http://www.ranchero.com/bigcat/) you can add your own contextual menu (right click) items. I don't think it comes with an Open Terminal Here applescript but I use this script (which I don't honestly remember if I wrote myself, or lifted from someone else's example):
on main(filelist)
tell application "Finder"
try
activate
set frontWin to folder of front window as string
set frontWinPath to (get POSIX path of frontWin)
tell application "Terminal"
activate
do script with command "cd \"" & frontWinPath & "\""
end tell
on error error_message
beep
display dialog error_message buttons ¬
{"OK"} default button 1
end try
end tell
end main
Similar scripts can also get you the complete path to a file on right-click, which is even more useful, I find.
It's a bit more than you're asking for, but I recommend Cocoatech's Path Finder for anyone who wishes the Finder had a bit more juice. It includes a toolbar button to open a Terminal window for the current directory, or a retractable pane with a Terminal command line at the bottom of each Finder window. Plus many other features that I now can't live without. Very mature, stable software.
http://cocoatech.com/
Ok, I realize that this is a bit late... maybe this alternative wasn't available at the moment of writing the post?
Anyway, I've found installing the pos package via Fink (a prerequisite in this case, maybe there is something similar for those who uses MacPorts?) to be the easiest solution. You get two commands:
posd - which gives the current directory of the frontmost Finder window (for which you presumably make an alias cdf=cd posd)
fdc - which switches the current directory of the frontmost Finder window to the Terminal pwd. This is slightly different from 'open .' which always opens a new finder window.
Yes, you have to switch to the Terminal window before writing cdf, but I suppose that's quite cheap comparing to clicking a button in the Finder toolbar. And it works with iTerm as well, you don't have to download a separate Finder toolbar button that opens an iTerm window. This is the same approach as proposed by PCheese, but you don't have to clutter your .bash_profile.
If like me you turn off the Finder toolbar, this Service adds an item to every folder's contextual menu: http://blog.leenarts.net/2009/09/03/open-service-here/
This also allows you to open any folder you see in Finder tree view.
I mostly use this function:
cf() {
cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')"
}
You could also assign a shortcut to a script like the ones below.
Reuse an existing tab or create a new window (Terminal):
tell application "Finder" to set p to POSIX path of (insertion location as alias)
tell application "Terminal"
if (exists window 1) and not busy of window 1 then
do script "cd " & quoted form of p in window 1
else
do script "cd " & quoted form of p
end if
activate
end tell
Reuse an existing tab or create a new tab (Terminal):
tell application "Finder" to set p to POSIX path of (insertion location as alias)
tell application "Terminal"
if not (exists window 1) then reopen
activate
if busy of window 1 then
tell application "System Events" to keystroke "t" using command down
end if
do script "cd " & quoted form of p in window 1
end tell
Always create a new tab (iTerm 2):
tell application "Finder" to set p to POSIX path of (insertion location as alias)
tell application "iTerm"
if exists current terminal then
current terminal
else
make new terminal
end if
tell (launch session "Default") of result to write text "cd " & quoted form of p
activate
end tell
The first two scripts have two advantages compared to the services added in 10.7:
They use the folder on the title bar instead of requiring you to select a folder first.
They reuse the frontmost tab if it is not busy, e.g. running a command, displaying a man page, or running emacs.
There is a bug in the AppleScript on OSX 10.6. (2 terminal windows open).
I fixed this by adding the close command after activate. This close the first Terminal window.
on run
tell application "Finder"
try
activate
set frontWin to folder of front window as string
set frontWinPath to (get POSIX path of frontWin)
tell application "Terminal"
activate
close
do script with command "cd \"" & frontWinPath & "\""
end tell
on error error_message
beep
display dialog error_message buttons ¬
{"OK"} default button 1
end try
end tell
end run

Resources