How to use Chrome as build in Sublime Text - build

I'm trying to set up Chrome as a build system in Sublime Text on MacOs.
Tried using the New build menu option and plugged in:
{
"/Applications/Google Chrome.app": "Chrome"
}
It appears in list after I restart as build option but doesn't work. Any ideas?

For reference, the build system documentation has lots of examples if you want to add more functionality. However, for now, we'll just make a simple build system with one command - open the current file in Chrome.
Each build system needs to have a "cmd" key - the command you want to run - and optionally a "selector" key - a rule for selecting which types of files you want to run. For now, we'll just assume you only want to run HTML files. Select Tools → Build System → New Build System… and replace the contens with this:
{
"cmd": ["/Applications/Google Chrome.app", "$file"],
"selector": "text.html"
}
Next, hit Save and save the file in the suggested folder with a name like Chrome.sublime-build. You don't need to restart for it to be visible in the Build System menu.
Now, when you want to view an HTML file in Chrome, you'll first select Tools → Build System → Chrome, then hit ⌘B to actually run the build. Chrome will stay selected as the current build system, so if you want to build again all you have to do is hit ⌘B.
If you do any work on Windows or Linux, the build command shortcut is CtrlB.

Related

Compile and execute cpp in xcode, and add additional execution instructions, Such as iconv command

Sorry, I'm new to Xcode and not very familiar with it, I use Xcode (command line tool project with external build system) to compile cpp files and automatically execute cpp unix executable files. After the program is compiled (command+R), I set the settings as shown in the screenshot below to automatically execute. Is there any way for me to execute also add additional commands?
Such as iconv.
The following line is what I ultimately want to execute.
./myFile argument1 | iconv -f big5
But my Xcode looks like it's executing only
./myFile argument1
really thanks
On the same place where you setup the build scheme, you can also add a post-build script.
Go to the left of the panel, and expand Build
Select Post-actions
Near the bottom center, click on + -> New Run Script Action
Add script like you would run them in terminal
Note the current directory will not be where the project is built
You can use ${TARGET_BUILD_DIR} macro for the build directory
Note, you want to make sure to select your current project at the Provide build settings from so it can import the correct path macros like TARGET_BUILD_DIR
A screenshot of adding a post-build script:
*Older versions of Xcode might have different GUI, but the idea should be about the same.
Sidenote, ⌘R is really for running the program within Xcode, consider using ⌘B.

How to get file path when opening a file from Finder

I have built a C++ cross-platform application and am struggling with how to get it to work correctly on macOS.
So far, I'm able to run the application manually with the command open /Applications/myApp.app --args /path/to/myFile.ply.
I have associated all ply files with my application but when I double click on it in the finder, the file path is not in argv argument list.
How can I get the double-clicked file path in my application?
You'll need an event loop, normally done using the NSApplicationMain function. Then you need to receive an Apple Event telling what file(s) to open. It will be much easier if you are willing to use some Objective-C or Swift, rather than pure C++. In Objective-C, you'd make an object that conforms to the NSApplicationDelegate protocol and that implements a method application:openURLs: or application:openFile:.
#JWWalker has explained how to solve your problem by altering your code to cope with the macOS GUI environment. Here is a different approach which avoids messing with your C++. Pick the one that suits you needs best.
If your cross-platform application is designed to run from the shell prompt you might want to run it within the macOS Terminal app. You can do this using a small AppleScript application which accepts the file paths passed by the Finder and invokes the Terminal app to run your C++ code.
To do this open Script Editor, you will find it inside Utilities in Applications. Enter the following:
on open passedItems
set convertedPaths to ""
# convert passed macOS paths to posix paths
repeat with nextItem in passedItems
set posixPath to the POSIX path of nextItem # convert macOS alias to posix path
set convertedPaths to convertedPaths & " '" & posixPath & "'" # place in quotes to protect any spaces
end repeat
tell application "Terminal" # open (if required) and activate Terminal
activate
do script "echo " & convertedPaths # just run echo - use the path to your C++ binary instead
end tell
end open
If you can program in C++ you can probably figure that out, if not search for AppleScript and all will become clear.
Now save this as an application, for this demo it was saved in /tmp/bridge (aka /private/tmp/bridge on macOS) as "Bridge.app".
Now create some test files with a suitable extension, e..g something like:
$ cd /tmp/bridge
$ touch a.bridgeDemo 'b c.bridgeDemo'
From Terminal you can open /tmp/bridge in a Finder window using:
$ open /tmp/bridge
In the Finder select any of your test files, do a Get Info and set the file to open with Bridge and then hit Change All...
Now try it: select the test files in the Finder and open them. You should see Terminal open/activate and show you something like:
$ echo '/private/tmp/bridge/a.bridgeDemo' '/private/tmp/bridge/b c.bridgeDemo'
/private/tmp/bridge/a.bridgeDemo /private/tmp/bridge/b c.bridgeDemo
$
Now edit the AppleScript to run your compiled C++ instead of echo and save it in a suitable location. HTH

How do I run a C++ Program in Sublime 3 (Ubuntu)?

So I have started to use Sublime Text 3 recently with my Ubuntu OS. I wanted to test it out so wrote a simple piece of c++ code. But when I try to build it does nothing, I have checked online and still nothing I even installed a build system (https://github.com/shikharkunal99/Sublime-Build-System) and still whenever I go to build it just opens open a black section at the bottom (see picture)
Install g++ to run c++ code
apt-get install g++
Then I will tell you a personal trick that I used. it is:
find | grep "part of your filename"
Replace "part of your filename" section with the name of the file or a part of the name of the file.
Suppose, the file name is Here.c. I type "Here" in place of part of your filename.
Then the final step, type
./a.out
Output is ready in front of you.
This post will help you in setting up Sublime Text 3 in a way that leads to a good workflow specifically for C++ programming environment (Ubuntu, GNU C++ Compiler) :
Note: Only the following step is essential for running c++ programs.
1. Create a Build System in Sublime Editor :
Sublime Text provides build systems to allow users to run external programs.
Go to Tools -> Build System -> New Build System.
Paste the following code in the file
{
"cmd": ["g++ -Wall -Wextra -O2 -pthread -H -std=c++17 \"${file}\" -o runfile && ./runfile <input.in> output.out"],
//above line works fine if input.in and output.out files are present in same directory in which .cpp file is present else add complete address of these files for using them as common input output files in your system.
"shell":true,
"working_dir":"$file_path",
"selector":"source.c,source.c++,source.cpp",
"variants": [
{
"name": "Variant Run",
"cmd" : ["gnome-terminal -- bash -c \"g++ $file_name ;echo ------------Output-------------; ./a.out;echo;echo; echo Press ENTER to continue; read line;exit; exec bash\""
],
}
]
}
Save the file (By default the file is placed in "~/.config/sublime-text-3/Packages/User" dir) something like "C++17.sublime-build" to differentiate it from the other build system files.
Create input.in and output.out text files in your working directory. This can be used for piping input from the input.in file, and output to the output.out file.
Note in the first line it uses the -std=c++17 flag to enable the latest features of C++17. If you don't want this or want to use C++14, replace this with the -std=c++14 flag.
Refer to https://linux.die.net/man/1/g++ for different compiler flags.
See Also https://discuss.codechef.com/t/are-any-compiler-flags-set-on-the-online-judge/1866
2. Setup window layout :
Create three new c++ file, file.cpp. Select View > Layout > Columns : 3. This will create three columns in the workspace. Select View > Groups > Max Columns : 2.
Write a hello world program & save inputs if any in the input.in file, and test its working. Use Shift+Ctrl+B and Select C++17 to build and execute the file (If selected C++17 - Variant Run it will execute the program in a separate terminal window like a normal program would).
The windows will look like this when you are done.
Layout Preview
3. Precompile headers :
Generally useful in competitive programming, we can speed up compilation time by precompiling all the header files as mentioned here, i.e. by precompiling the bits/stdc++.h header file.
For this, first, navigate to the stdc++.h file. This will be located at a directory similar to ~/usr/include/x86_64-linux-gnu/c++/9/bits Open terminal window here.
Run the command sudo g++ -std=c++17 stdc++.h, to compile the header. Take care to use the same flags you used in your build system. Check to make sure that the stdc++.h.gch file was created in that directory.
4. Sublime Text features :
Snippets & Completion
Read up on the documentation of snippets and completions at the official guide.
5. Other Features :
Read https://scotch.io/bar-talk/best-of-sublime-text-3-features-plugins-and-settings
This program works perfectly fine for me using Build 3120, and I expect it will work fine with previous builds. First, you need to select Tools → Build System → C++ Single File (Tools → Build System → Automatic should also work, but I prefer to be explicit). Then, either hit CtrlShiftB or select Tools → Build With… and select C++ Single File - Run. This will compile your .cpp file to an executable in the same directory as the source file, then run it.
Well I also got various issues with this thing finally I got an amazing thing in the package control pallet.Follow the instructions:
1.Open up the Package control Pallet
2.Search for C++ Builder
3.You will see C++ Builder-Mingyang Yang
4.click it and then wait for a couple of seconds
5.finally go to tools->build system->select C++ Builder-Mingyang Yang
6.finally tap the Shift+Ctrl+B and then select C++ Builder-Mingyang Yang Build and Run
7.finally here you go you can not only build this but also use the console for input
Note:This will execute only when there is gcc compiler included in the terminal otherwise at first install gcc by the command apt-get install gcc then you can use c++

Sublime Text 2 build via simple batch file

I've been playing a bit with ST2 and it seems like a pretty cute editor. Unfortunatelly, its documentation is horrible.
And I'm being nice. So here's my question.
I have five files in a directory, which I usually build via a .bat file with
ifort file1.f90 file2.f90 file3.f90 ...
how can I define and execute this line on windows cmd (taking account the enviromental variables like PATH) from ST2 via a shortcut and see the output? Is something like that even possible at this stage with ST2?
I just made a new build like this:
{
"cmd": ["$file"],
"selector": "source.dosbatch"
}
Then you would put your ifort ... string in a .bat file and "build" that.
Here's a step by step way to run a batch file as part of your build process:
In Sublime Text 2 go to Tools -> Build System -> New Build System
You'll be presented with a new text file with the following code in it:
{
"cmd": ["make"]
}
Now change the "make" to the exact path of your batch file so it looks something like this:
{
"cmd": ["D:\\xampp\\htdocs\\myproject\\dostuff.bat"]
}
Notice you must use double backslash for windows paths.
Then save this file out as myproject.sublime-build in the default directory it asks you to save it in (should be $HOME_DIR/AppData/Roaming/Sublime Text 2/Packages/User).
Then in your project go back to Tools -> Build System and select the build system that is the same name as the file you just created (in this case it's "myproject").
Hit ctrl + b or go to tools -> build and your batch file should run and output will appear in the ST2 console.
This worked for me and works with paths and files with whitespaces.
I have fixed a bug which I've posted on the ST forum here and this here includes that fix.
Paste this into your Batch.sublime-build file.
This will run cmd.exe and run the code in its native console. This will accept your inputs of the batch file.
{
"file_patterns": ["*.bat", "*.cmd"],
"selector": "source.batch",
// This runs the batch file in the cmd window.
"shell_cmd": "start \"${file_name}\" call \"${file}\""
}
Here's a build that can be saved as BatchStConsole.sublime-build
This will run the code in Sublime Texts' console. This will not accept your inputs of the batch file. But still useful for debugging as it passes any arguments like the native CLI but just no interaction.
{
"file_patterns": ["*.bat", "*.cmd"],
"selector": "source.Batch",
// This outputs to Sublime Texts' console
"shell_cmd": "\"${file}\""
}
Also, in a new file ...\Data\Packages\User\Batch File.sublime-settings you can then place this code and save. This will build these filetypes when you have automatic build as your build detection.
{ "extensions": [ "bat", "cmd" ] }
Relevant help:
https://ss64.com/nt/start.html
https://docs.sublimetext.io/guide/usage/build-systems.html
https://www.sublimetext.com/docs/3/build_systems.html

Where to start for writing a shell script for copying elements into main app xcode4

I am looking for some documentation or tutorial for copying files from a given directory into the app created by xcode at build time, before it is run.
At first I have tried to copy files into the derived directory, hoping that everything resides in there would be automatically added to the app, but I was wrong.
So I am looking for a script because the original dir may change its name, second the script could be customized by another xcode 4 user with its src dir path etc.
The things is I don't know how to start, which language etc. I am quite confident with shell script, but maybe there's a better option.
Second, I am trying to figure out which command could add a file in the already built app.
thanks
That answer didn't really help - the BUILT_PRODUCT_DIR isn't where most stuff goes.
Ultimately, I found you just need to do:
Add the following to the very end of your script (or get your script to write directly to the output location):
cp ${DERIVED_FILE_DIR}/[YOUR OUTPUT FILES] ${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}
...but there's a lot of other things I tried. More thoughts and ideas here: http://red-glasses.com/index.php/tutorials/xcode4-a-script-that-creates-adds-files-to-your-project/
You want a Run Script or Copy Files build phase. Select your main project in the navigator, then select the app's target. Click the Build Phases tab. Click the Add Build Phase button at the bottom of the window and choose the appropriate phase.
By "appropriate" I mean if you really want to run a script, you'll use a Run Script build phase and use Xcode-provided environment variables like $BUILT_PRODUCT_DIR (see the documentation or hit build and examine the full output of an empty script in the build log) to figure out your target folder. If all you want to do is copy files (no real processing), the Copy Files build phase already knows how to locate the app bundle's proper folders depending on what you're copying (Resources, Frameworks, etc.).