Xcode - open current file in a NEW window (a real challenge !) - xcode

Say you have File.m showing in the blue area above.
I want File.m to open in a new separate window.
How to do this??
Note that - of course - IF you have the ProjectNavigator showing and IF you can see the filename "File.m", then double-click on the filename and it will open in a new window.
What I want to do is open ANOTHER copy of the "CURRENT" file (the one in the primaryEditor) in a NEW WINDOW - without touching the ProjectNavigator.
Any ideas? Cheers
Note that ... (an excellent page here)
http://realmacsoftware.com/blog/xcode-4-s-less-obvious-shortcuts
if you click on whatever file is in the current primaryEditor, then shift option apple comma that will indeed open the following "freaky little-known Xcode popup" ...
you can then, indeed, choose to open a new window. (So to be clear, that is exactly what I want to do.)
So, I want to be able to do that in one step. Cheers!

In XCode's Preferences you can change the following:
And then right-click within the primary editor and click "Open in Separate Window"
Edit:
Instead of right clicking in the primary editor you can simply type command+option+, (comma)

Open the file what you want to open in the NEW window.
Command + Shift + t
Open the file what you want to open in the separated window.
Command + Option + ,

I wrote a new automator service that does the following steps in Xcode:
Execute the default shortcut for the "Open in..." menu item (Command-O)
(if this isn't your current "Open in" shortcut, the workflow will not work. If you still want to test it, just changed your default shortcut :) )
Go all the way to the left in the weird "Open in" view
hit Enter
Current file opened in new window
The only thing you need to do is install the service on osx and map a shortcut to it.
Here a download to the service:
https://www.dropbox.com/s/486t2iz17o0l9dh/Open-New-Window.zip
INSTALL INSTRUCTIONS
1.Place Service file in folder ~/Library/Services
2. Open "System preferences"
3. > Keyboard
4. > Tab "ShortCuts" > "Service" > "General"
5. > Set your own shortcut for the service
6. Go to XCode and test the service by using your very own shortcut
Tested on OS X Mavericks, Xcode 5.0.1

In XCode 4 you can have a split window via View -> Show Assistant Editor. Then choose the same file which you have already opened in the Primary editor by the small right arrows on top of Assistant Editor.
Thanks

in the ProjectNavigator. If you see the file name: Double click on the file name. It will open in a new window.

Related

Open a folder in vscode through Finder in macOS?

On my windows pc, I would just double click a folder and click "Open in VS Code" to a open a folder, which saved time for me when looking through lots of code online. With Mac, however, I do not have this option. I have to directly open vscode and click "Open" to open a new folder. How do I directly open a file in vscode through finder in macOS?
2023 Update - Use Native Method
Open the Command Palette (Cmd+Shift+P) and type shell command to find the Shell Command: Install 'code' command in PATH command.
Restart all terminal sessions for the new $PATH value to take effect.
You'll be able to type code . in any folder to start editing files in that folder.
If you want to do it in Finder, you can write an Automator script to do it (it's easier than it sounds, AND shows you the power of the OS).
Launch Automator
Create New Document
Create a new Quick Action
Add the Action... (New Method - 10.13+)
Workflow receives current files or folders from Finder.
Add a new "Open Finder Items" action to the workflow. (drag the "Open Finder Items" object, highlighted in the screenshot, to the empty window on the right)
Select "Visual Studio Code" from the list.
Add the Action... (Old Method)
Workflow receives current files or folders from Finder.
Add a new Run Shell Script action to the workflow. (drag the "Run Shell Script" object, highlighted in the screenshot, to the empty window on the right)
Configure the Workflow (Old Method)
Set the Pass Input to be as arguments
Paste the following in the input box:
open -n -b "com.microsoft.VSCode" --args "$*"
Save the action using a name like Open in Visual Studio Code.
You may now right-click on the folder and find your newly created task under Quick Actions.
The simplest solution is to create a Quick Action with Open Finder Items:
This way you don't need a shell script that might break with an OS update or VS Code update
Launch Automator and select Quick Action or File > New > Quick Action If Automator is already open.
Set Quick Action receives selected to files or folders and in to Finder.
Choose an Image (icon) select Choose.. and double click Visual Studio Code in the window that pops up, this will set your quick action icon to the same as VS Code
Add Open Finder Items, Choose open with as Other.. select Visual Studio Code and save your Quick Action as Open in VSCode optionally choose an Image for your Quick Action job done.
Finally just hit Cmd+S to save and name it Open with VSCode
Well you need to understand. macOS has different ways to do things than windows and initially it might be a challenge. For starters you start to make use of Terminal. its beautiful.
Now answering your question.
Open your VS code and then, press CMD + SHIFT + P, type shell command and select Install code command in path. Afterwards, navigate to any project from the terminal and type code . from the directory to launch the project using VS Code.
Using the new shortcuts app.
Download:https://github.com/gluedpixel/shortcuts
Or create it by yourself
Open Shortcuts app > Quick Actions
Change from receive any to Files and Folders
Add run shell script action
Paste the code open -n -b "com.microsoft.VSCode" --args "$*"
Change input to Shortcut input
Click on Shortcut input and set type to "Folder" & Get to File Path
There are some ways suggested here in the VS Code GitHub Issues tracker, but I would go with the first option of dragging the folder onto the app icon if you have VS Code icon in your dock!
If you really want to be able to do so from a right click, then this repository has a workflow solution. https://github.com/Sankra/OpenFolderInVSCode
Drag and drop the folder from Finder onto an empty VSCode window.
Found a way to achieve a 'Quick Action' that met my needs using the following steps using MacOS' Shortcuts app (No Automator needed):
if you are looking for screenshots as well, I wrote a small Notion page
Open the ‘Shortcuts’ app on your Mac
On the Left Pane, navigate to ‘Quick Actions’
On the top bar, press the ➕ button to add New Shortcut.
Click on ‘Any’ and change to ‘Files and Folders’ by selecting only those from the dropdown that appears on clicking the ‘Any’ field.
Click on ‘Action Library’ on the right pane and search for ‘shell’. Drag and drop the ‘Run Shell Script’ onto the main pane. This will add the action to the Shortcut’s flow.
In the shortcut details (on the right pane), make sure Use as Quick Action is ticked and so are ‘Finder’ and ‘Service Menu’.
In the newly added action called ‘Run Shell Script’ write “code “ and right-click to get the below pop-up, go ahead ‘Insert variable’ and then ‘Shortcut Input’.
Click on the ‘Shortcut Input’ tag that appears. A pop must appear, choose ‘File Path’ and just click anywhere else. This will change the tag to ‘File Path’.
Set ‘Pass Input:’ option to ‘as arguments’.
Be sure to set a cool name for your new Shortcut! I named mine a boring “Open in VSCode”.
Open a new ‘Finder’ Instance, right-click on a folder, go to ‘Quick Actions’ > ‘Customize...’
Make sure your Shortcut’s Name is selected. Exit the dialogue and Test!
There is a Terminal command named code, I would stick with it and you should get used to it as well if you are regularly using VS.
Follow the one-time setup with-in VS Code
Open your VS code and press ⌘ CMD + SHIFT + P
Type shell, and select Shell Command: Install 'code' command in PATH from the list
Open Terminal and hit any of the commands below:
code <path-to-folder>
OR
cd <path-to-folder>
code .
OR
cd <path-to-folder> && code .
I've actually managed to have it as an icon you can just add to the Finder window as such:
The script will allow you to open the current folder path in VSCode.
The steps to do so are:
Create a new Application with Automator
Select the "Run shell script" from the menu and drag it to the editor
Paste the following small script to the text area:
finderPath=`osascript -e 'tell application "Finder" to get the POSIX path of (target of front window as alias)'`
open -n -b "com.microsoft.VSCode" --args "$finderPath"
Save the Application to the Applications folder
Right click the app and drag and drop your desired VSCode icon to the blank area to the left of the name and close the Get Info window
Press and hold the Command key and drag the Application to an empty space in the Finder title row like in the picture
Test that it works by pressing on the icon. VSCode should now open with the content of the current folder in the navigation bar
Here is an alternative derived from the accepted answer.
Indeed the accepted solution by #jnovack just opens VS Code for me, but not the desired folder. If VS Code is already running it just switches the focus to a running instance of VS Code.
Solution
Provided that you have the code CLI command installed (if not, go to VS Code and do CMD+SHIT+P>"Shell Command: install 'code' in PATH")
Then open a terminal and run which code. You should get the code executable path looking somethin like /usr/local/bin/code
Then, as per #jnovack 's answer,
Open Automator
Create a new Document (CMD+N)
Create a new Quick Action
Workflow receives current files and folders from Finder.
Add a new Run Shell Script action to the workflow. (drag and drop the "Run Shell Script" object)
Pass input as arguments
Paste this code in the shell command field /usr/local/bin/code -n "$*" or replace with your path to the code executable if it differs from this one.
Save the action
Tbh. I would have expected to be able to use just code -n "$*", but for some reason, the PATH used by the automator's shell seems to be different from that used in the terminal. I'd welcome some input on that matter.
I find this to be the best solution out there:
https://github.com/RoadToDream/SzContext

How to define a shortcut key for opening a terminal in Mac OS [duplicate]

One of my primary tools used for programming is my Terminal. It makes my programming process more efficient when I'm able to quickly open a Terminal window.
In Ubuntu, I was using (window+Alt+T) to open Terminal. But now I use a Macbook at my programming job.
Sometimes I use Spotlight to search "Terminal", and press Enter.
I'd like to know if I can assign a keyboard hotkey to do it.
I tested the following procedure under macOS Mojave 10.14.6 (18G3020).
Launch Automator. Create a document of type “Quick Action”:
(In older versions of macOS, use the “Service” template.)
In the new Automator document, add a “Run AppleScript” action. (You can type “run applescript” into the search field at the top of the action list to find it.) Here's the AppleScript to paste into the action:
on run {input, parameters}
tell application "Terminal"
if it is running then
do script ""
end if
activate
end tell
end run
Set the “Workflow receives” popup to “no input”. It should look like this overall:
Save the document with the name “New Terminal”. Then go to the Automator menu (or the app menu in any running application) and open the Services submenu. You should now see the “New Terminal” quick action:
If you click the “New Terminal” menu item, you'll get a dialog box:
Click OK to allow the action to run. You'll see this dialog once in each application that's frontmost when you use the action. In other words, the first time you use the action while Finder is frontmost, you'll see the dialog. And the first time you use the action while Safari is frontmost, you'll see the dialog. And so on.
After you click OK in the dialog, Terminal should open a new window.
To assign a keyboard shortcut to the quick action, choose the “Services Preferences…” item from the Services menu. (Or launch System Preferences, choose the Keyboard pane, then choose the Shortcuts tab, then choose Services from the left-hand list.) Scroll to the bottom of the right-hand list and find the New Terminal service. Click it and you should see an “Add Shortcut” button:
Click the button and press your preferred keyboard shortcut. Then, scratch your head, because (when I tried it) the Add Shortcut button reappears. But click the button again and you should see your shortcut:
Now you should be able to press your keyboard shortcut in most circumstances to get a new terminal window.
As programmers we want the quickest, most fool-proof way to get our tools in order so we can start hacking. Here are how I got it to work in MacOS 10.13.1 (High Sierra):
Option 1: Go to System Preferences | Keyboard | Shortcut | Services.
Under Files and Folders section, enable New Terminal at Folder
and/or New Terminal Tab at Folder and assign a shortcut key to it.
Option 2: If you want the shortcut key to work anywhere, create a new Service using Automator, then go to the Keyboard Shortcut to assign a shortcut key to it. Known limitation: not work from the desktop
Notes:
If the shortcut doesn't work, it might be in conflict with another
key binding (and the OS wouldn't warn you), try something else, e.g.
if ⇧⌥T doesn't work, try ⇧⌘T.
Don't spell-correct MacOS, that's not necessary.
iTerm2 - an alternative to Terminal - has an option to use configurable system-wide hotkey to show/hide (initially set to Alt+Space, disabled by default)
There is a third party app called hotkey app which can launch apps based on the configured shortcuts.
You can install it and setup required shortcut.
https://codenuts.de/en/posts/hotkey/
As others mentioned already, use the keyboard shortcut from services. I also had to restart the computer to take into effect.
mac - "command + t" will open a new terminal window.
Try command + t.
It works for me.

Is there a keyboard shortcut (hotkey) to open Terminal in macOS?

One of my primary tools used for programming is my Terminal. It makes my programming process more efficient when I'm able to quickly open a Terminal window.
In Ubuntu, I was using (window+Alt+T) to open Terminal. But now I use a Macbook at my programming job.
Sometimes I use Spotlight to search "Terminal", and press Enter.
I'd like to know if I can assign a keyboard hotkey to do it.
I tested the following procedure under macOS Mojave 10.14.6 (18G3020).
Launch Automator. Create a document of type “Quick Action”:
(In older versions of macOS, use the “Service” template.)
In the new Automator document, add a “Run AppleScript” action. (You can type “run applescript” into the search field at the top of the action list to find it.) Here's the AppleScript to paste into the action:
on run {input, parameters}
tell application "Terminal"
if it is running then
do script ""
end if
activate
end tell
end run
Set the “Workflow receives” popup to “no input”. It should look like this overall:
Save the document with the name “New Terminal”. Then go to the Automator menu (or the app menu in any running application) and open the Services submenu. You should now see the “New Terminal” quick action:
If you click the “New Terminal” menu item, you'll get a dialog box:
Click OK to allow the action to run. You'll see this dialog once in each application that's frontmost when you use the action. In other words, the first time you use the action while Finder is frontmost, you'll see the dialog. And the first time you use the action while Safari is frontmost, you'll see the dialog. And so on.
After you click OK in the dialog, Terminal should open a new window.
To assign a keyboard shortcut to the quick action, choose the “Services Preferences…” item from the Services menu. (Or launch System Preferences, choose the Keyboard pane, then choose the Shortcuts tab, then choose Services from the left-hand list.) Scroll to the bottom of the right-hand list and find the New Terminal service. Click it and you should see an “Add Shortcut” button:
Click the button and press your preferred keyboard shortcut. Then, scratch your head, because (when I tried it) the Add Shortcut button reappears. But click the button again and you should see your shortcut:
Now you should be able to press your keyboard shortcut in most circumstances to get a new terminal window.
As programmers we want the quickest, most fool-proof way to get our tools in order so we can start hacking. Here are how I got it to work in MacOS 10.13.1 (High Sierra):
Option 1: Go to System Preferences | Keyboard | Shortcut | Services.
Under Files and Folders section, enable New Terminal at Folder
and/or New Terminal Tab at Folder and assign a shortcut key to it.
Option 2: If you want the shortcut key to work anywhere, create a new Service using Automator, then go to the Keyboard Shortcut to assign a shortcut key to it. Known limitation: not work from the desktop
Notes:
If the shortcut doesn't work, it might be in conflict with another
key binding (and the OS wouldn't warn you), try something else, e.g.
if ⇧⌥T doesn't work, try ⇧⌘T.
Don't spell-correct MacOS, that's not necessary.
iTerm2 - an alternative to Terminal - has an option to use configurable system-wide hotkey to show/hide (initially set to Alt+Space, disabled by default)
There is a third party app called hotkey app which can launch apps based on the configured shortcuts.
You can install it and setup required shortcut.
https://codenuts.de/en/posts/hotkey/
As others mentioned already, use the keyboard shortcut from services. I also had to restart the computer to take into effect.
mac - "command + t" will open a new terminal window.
Try command + t.
It works for me.

Xcode 4: Open arbitrary files in assistant editor

Is it possible to open a file that is not part of any project in the assistant editor? Open Quickly can open files in the assistant, but only searches the project itself. If I open an outside file in Xcode, it gets a new window that cannot access the project in its assistants.
With the assistant editor open, drag the file from the left-hand list onto the Jumplist at the top of the assistant editor (where the currently open file is).
Yes, it's possible.
#What. Your answer only works if the file is in the project. You can only drag a file to the Jumplist at the top of the assistant editor if the file is displayed within the file list on the left. This means you would have to add the file to the project before opening. Sometimes, this is not desirable as you may want to open a file from another project for reference while coding.
Steps that I use are: (Tested in Xcode 6 and Xcode 7 Beta)
In Xcode, make sure your Tab Bar is shown. If it is not, click on "View -> Show Tab Bar" in the top menu
Open the file... Go to "File -> Open". Select the arbitrary file you want to open. This will open the file in a new window. (Screenshot 1 below)
Drag the tab in the new window to the tab bar in your main editor. Now, you will have 2 tabs in your main editor and your new window will be gone (Screenshot 2 below)
Next, click on your original tab (Where your main and assistant editor is) and the file will be available in the recent files list. Simply select the file from the recent files list in the assistant editor and you now have the file open in the assistant editor. You can also close the other tab as you no longer need it. (Screenshot 3 below)
Screenshot 1. Simply drag the tab from the new window to the main tab bar.
Screenshot 2, you will now have 2 tabs.
Screenshot 3. Back at your original tab, you now have the option to open the new file within the recent files list in the assistant editor. Click it and you are done.
Screenshot 4. Final result... we have our arbitrary file open in the assistant editor.
Here is what I do (Xcode 9.3) :
Add wanted file to the Xcode project
Open assistant editor with that
file
delete this file (references only) from the Xcode project. Xcode will
remove the file from the assistant editor.
Now go to the assistant
editor "recent files", the file will be there. Select it.
My preferred way to do this is to #include whatever file I want to open in some C file, like this:
#include "/home/julius/path/to/file"
Command-clicking the path will open the file. It's a shame Xcode forces you to use this workaround.
You can drag an arbitrary file on the assistant editor. Then it will be opened there.

How to change file associations in Mac OS X

So I used Xcode and all. Now, whenever I open my text files it opens automatically with Xcode. How do I undo this?
RightClick on the file name -> Open With -> Choose the editor you wish to open with this kind of file. And that sets to be default editor for opening files with that particular file extension ( i.e., .txt ) when opened in future.
Edit: The above procedure is not a permanent one. Try this -
Right click on the file name.
Choose Get Info
Choose one from the Open With drop down. This will be set as default application and is used in future if opened the .txt file.
Right-click any text-file an select "Get-info", then choose your preferred Text-editor under "Open with" and click "Change all..."
You can change program association by right clicking on your file and clicking on "Get Info". This will bring up another dialog in which you can change the program associated with that file type.
You can find visual instructions here: http://osxdaily.com/2009/10/25/change-file-associations-in-mac-os-x/

Resources