Xcode and javascript/php/html development?

Hi,

1) Can I create a custom javascript or php template (or are there ones available?) and then have Xcode open a file/project with that template? I'd like to try using Xcode to code javascript/html/php files.

2) Also, where are the templates stored? I want to change a C++ template. Currently, the template I am interested in changing launches like this:


#include <iostream>
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, world!\n";
return 0;
}


and I want to change it to:


#include <iostream>
using namespace std;
int main ()
{
return 0;
}


Thanks.

17" intelimac 2ghz 2gb 250gb, Mac OS X (10.4.7)

Posted on Feb 8, 2007 2:19 PM

Reply
14 replies

Feb 8, 2007 10:57 PM in response to 7stud

Hi--

Look in /Library/Application Support/Apple/Developer Tools. The templates for files are in " File Templates" in that folder. Look at the shell script one for an example you should be able to build on. Make sure you edit the plist file, too. The edits should be obvious, but post back if they're not.

Pay attention to how the directory structure works, too. If you have a folder with the extension " .pbfiletemplate", that's your template, but if it doesn't have that extension, the file name will be a bold header (like BSD or WebObjects) in the New File assistant.

The C++ template is in " Project Templates" in that same area, in the " Project Templates" folder. I'd suggest making backups of any files you change in there before you fool around with them. Also, once you make changes and get the templates like you like them, back those up, too, in case Apple decides to overwrite those files in a future update.

charlie

Feb 9, 2007 1:39 AM in response to Charles Minow

Hi,

Thanks for the response.

Look in /Library/Application
Support/Apple/Developer Tools
. The templates for
files are in " File Templates" in that
folder. Look at the shell script one for an example
you should be able to build on. Make sure you edit
the plist file, too. The edits should be
obvious, but post back if they're not.


I see some hieroglyphics in Shell Script.pbfiletemplate that appear to dynamically get the username, and date, etc. I don't need anything like that.

Pay attention to how the directory structure works,
too. If you have a folder with the extension
" .pbfiletemplate", that's your template, but
if it doesn't have that extension, the file name will
be a bold header (like BSD or
WebObjects) in the New File assistant.


Ok, I figured that part out. I added a Javascript folder to the File Templates folder. Then inside the Javascript folder, I created a folder called Javascript File.pbfiletemplate. And inside Javascript File.pbfiletemplate, I created a file called file.html with the skeleton code I want.

Now in Xcode when I click on File>New File, there is a Javascript heading with the sub heading Javascript File. However, when I select Javascript File, and then on the next page give the file a name, and then click on Finish, the file doesn't open up in Xcode--nothing happens. What I would like is for the file to open in Xcode so that I can write code and have the lines indent automatically. Do I need a .plist file? How do I make one?

I would also like, if possible, to make an auto completion file for the Javascript File type.

The C++ template is in " Project Templates"
in that same area, in the " Project
Templates
" folder. I'd suggest making backups of
any files you change in there before you fool around
with them. Also, once you make changes and get the
templates like you like them, back those up, too, in
case Apple decides to overwrite those files in a
future update.


When I opened up Xcode today, the C++ template somehow changed to what I wanted--but I've never looked inside those template folders before. Maybe inside Xcode, I somehow navigated to the templates and changed the C++ template without knowing it. I don't really know.

Feb 9, 2007 2:11 AM in response to Charles Minow

Another question: I'm trying to write a hello world file io program in C++, but I can't seem to get the pathname to the file correct. I'm using

ifstream inFile( "~/Documents/xcode stuff/TestData/data.txt");

to open the file. But that's not working.

edit: Got it. The path is:

ifstream inFile("/Users/<username here>/Documents/xcode stuff/TestData/data.txt");

Select the file and click on File>Get Info to get the path of a file. Unfortunately, you can't copy and paste the path from the path info window. Stupid.

Feb 9, 2007 12:51 PM in response to 7stud

I'd suggest making backups of
any files you change in there before you fool around
with them.


lol. Ok, now I've somehow corrupted my C++ project template. I can still execute C++ programs, but upon exit instead of saying:

test2 has exited with status 0.


where test2 is the project name, now it says:

«PROJECTNAME» has exited with status 0.


It looks like I deleted all the comments preceding the skeleton code in the C++ template located here:

Library/Application Support/Apple/Developer Tools/Project Templates/Command Line Utility/C++ tool/main.cpp

(Is there anyway to copy and paste the path of a file? I had to type that by hand.)

In an effort to correct the C++ template, I went into the Java template and copied the comments there:


//
// «PROJECTNAMEASIDENTIFIER».java
// «PROJECTNAME»
//
// Created by «FULLUSERNAME» on «DATE».
// Copyright (c) «YEAR» «ORGANIZATIONNAME». All rights reserved.
//


and pasted them into the main.cpp file in the C++ template with the following change:


//
// «PROJECTNAMEASIDENTIFIER».cpp <---------</font>
// «PROJECTNAME»
//
// Created by «FULLUSERNAME» on «DATE».
// Copyright (c) «YEAR» «ORGANIZATIONNAME». All rights reserved.
//

but that didn't work.
17" intelimac 2ghz 2gb 250gb Mac OS X (10.4.7)

Feb 9, 2007 3:08 PM in response to 7stud

Hi--

Here's the full text of the C++ template file.cpp in that directory. Note that there's no " .cpp" at the end.

<pre class="command">/*
* «FILENAME»
* «PROJECTNAME»
*
* Created by «FULLUSERNAME» on «DATE».
* Copyright «YEAR» «ORGANIZATIONNAME». All rights reserved.
*
*/

#include "«FILEBASENAME».h"</pre>

(Is there anyway to copy and paste the path of a
file? I had to type that by hand.)


Since I spend a lot of my time in the Terminal with a shell, I just use pwd (print working directory) at the prompt. There might be other ways, but I don't know them...

I'll post back later with more instructions on how to set up a template, including what to look for in the plist file.

charlie

Feb 9, 2007 3:46 PM in response to Charles Minow

Here's the full text of the C++ template file.cpp in that directory. Note that there's no ".cpp" at the end.

Wait a minute. I don't have a file.cpp in that directory (..../Project Templates/Command Line Utility/C++ Tool). I also checked the Java folder inside the Project Templates folder. Inside the Java folder, there is a Java Tool folder, and inside that folder is a file called JavaTool.java, which is where I copied the comments from. JavaTool.java looks like this in its entirety:


//
// «PROJECTNAMEASIDENTIFIER».java
// «PROJECTNAME»
//
// Created by «FULLUSERNAME» on «DATE».
// Copyright (c) «YEAR» «ORGANIZATIONNAME». All rights reserved.
//
import java.util.*;
public class «PROJECTNAMEASIDENTIFIER» {
public static void main (String args[]) {
// insert code here...
System.out.println("Hello World!");
}
}


Inside the Java Tool folder there is no file named file.xxx either.

I tried adding your comments to main.cpp inside the Command Line/C++ Tool folder, but when I create a new project, I have the same problem.

Feb 9, 2007 7:05 PM in response to 7stud

Wait a minute. I don't have a file.cpp in that
directory (..../Project Templates/Command Line
Utility/C++ Tool).


Okay, we're talking about two different places. I thought you just wanted to make a Javascript file template and you're poking around in the project templates. I'm not sure what the point of making a project template would be for a Javascript file. There's no real "build" setting, you'd just be using the Xcode text editor. But all your debugging would need to be done with a web browser. So I was talking about looking at the shell script file template that's here:

<pre class="command">/Library/Application Support/Apple/Developer Tools/File Templates/Scripts/Shell Script.pbfiletemplate</pre>That's the template you'd get if you were to choose "New File..." from the "File" menu in Xcode. That's what you'd need to do for a JavaScript file.

For your project templates, it looks like they're laid out a bit differntly. There are three files:

<pre class="command">-rw-rw-r-- 1 root admin 3132 Sep 11 22:03 C++Tool.1
drwxrwxr-x 4 root admin 136 Sep 11 22:03 C++Tool.xcodeproj
-rw-rw-r-- 1 root admin 143 Sep 11 22:03 main.cpp</pre>There, main.cpp is the file. On my system, it doesn't have all the extra stuff. This is all there is:

<pre class="command">#include <iostream>

int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}</pre>It looks like the project name fill-ins are done are done via another file, the C++Tool.1 file. That looks like an mdoc file. I don't know anything about it, but it's just a text file, so you can look at it in any text editor.

Inside the Java Tool folder there is no file named
file.xxx either.


Right, because it's called " JavaTool.java." When you create a new project, Xcode assumes you're starting from scratch, so it makes sense to have a main.cpp file in your project. Or a JavaTool.java one. The file templates, however, are set up differently, because Xcode assumes you either don't want a project file, or you're adding your new file to an already existing project.

I tried adding your comments to main.cpp inside the
Command Line/C++ Tool folder, but when I create a new
project, I have the same problem.



At this point, unless you can puzzle out the C++Tool.1 file, you might be best off to use Pacifist to extract the default template out of your Xcode installer package. It's $20 shareware, but at some point, you'll be glad you've got it if you do a lot of tinkering with your system.

A page with some good information on the project templates, by the way, is at cocoadev. Obviously, it's a bit skewed toward the Cocoa user, but you should be able to get some good info off that page.

charlie

Feb 9, 2007 7:24 PM in response to Charles Minow

As promised: to create a new JavaScript template, you need to follow these steps:

1. Navigate to this folder:

<pre class="command">/Library/Application Support/Apple/Developer Tools/File Templates/Scripts</pre>

2. Duplicate the Shell Script.pbfiletemplate folder and rename it to JavaScript.pbfiletemplate.

3. Open the folder and rename file.sh to file.js and replace its contents with anything you'd like to appear in every file. If you want to mess around with the macros like «FULLUSERNAME», you'll need to change Xcode's default encoding from the default to UTF-8, I think.

4. Next to your file.js file, there's another file called TemplateInfo.plist. If you double-click it, the Property List Editor should open it. Click on the triangle next to "Root" to reveal your two key/value pairs. The Description value can be anything, but it probably makes sense to change it to " An empty JavaScript script file." The MainTemplateFile key's value should change from " file.sh" to " file.js". Save the plist and exit the editor.

5. Go back to Xcode and choose "New File..." from the "File" menu. Scroll down to the "Scripts" section and choose your new Javascript template. It should just work.

charlie

Feb 9, 2007 10:42 PM in response to Charles Minow

Me:
Ok, I figured that part out. I added a Javascript folder to the File Templates folder. Then inside the Javascript folder, I created a folder called Javascript File.pbfiletemplate. And inside Javascript File.pbfiletemplate, I created a file called file.html with the skeleton code I want.
Now in Xcode when I click on File>New File, there is a Javascript heading with the sub heading Javascript File. However, when I select Javascript File, and then on the next page give the file a name, and then click on Finish, the file doesn't open up in Xcode--nothing happens. What I would like is for the file to open in Xcode so that I can write code and have the lines indent automatically. Do I need a .plist file? How do I make one?


You:
Next to your file.js file, there's another file called TemplateInfo.plist. If you double-click it, the Property List Editor should open it. Click on the triangle next to "Root" to reveal your two key/value pairs. The Description value can be anything, but it probably makes sense to change it to "An empty JavaScript script file." The MainTemplateFile key's value should change from "file.sh" to "file.js". Save the plist and exit the editor.


Ok. I added a plist file by copying one from another template and then editing it. I clicked on an arrow on the left to expand the entries, and then I double clicked on the text in the value column to edit the value.

Adding a plist file now makes the Javascript File type launch in an Xcode editor. However, similar to when I choose File>New Empty File, Xcode will not indent the code I type in. Here is an example:

if(true)
{
this line not indented.


My Xcode indenting preferences don't seem to have any effect. For instance, I can select "automatically insert closing brace" and it has no effect in the Javascript File type (nor in the File>New Empty File).

More tests: If I click on File>New Empty File and save it with a .cpp extension, then the indenting works. If I save it as a .js file, then the indenting works. But if I save it as a .html file, the indenting won't work. I'd like to have the indenting work on a .html file type. Is that possible?

Feb 9, 2007 11:47 PM in response to 7stud

Hi--

If I save it as a .js file, then the
indenting works. But if I save it as a .html file,
the indenting won't work. I'd like to have the
indenting work on a .html file type. Is that
possible?


Sure. I did a quick Google search and came up with a page that details how to do it for Pascal. So, working from that, I came up with this:

1. Create a new, empty text file. Now, open this file:

<pre class="command">/System/Library/PrivateFrameworks/DevToolsCore.framework/Versio ns/A/Resources/Built-in languages.pblangspec</pre> Copy out the definition for HTML files:

<pre class="command"> // HTML
{
Identifier = html;
Name = "HTML/XML";
Description = "HTML and XML";
BasedOn = "pbx rootlanguage";
SupportsIndentation = NO;
Indentation = {
};
SyntaxColoring = {
CaseSensitive = NO;
UnicodeSymbols = YES;
MultiLineComment = (
( "<!--", "-->" )
);
/*KeywordDelimeters = ( // not supported yet
( "<"<">" )
);
Character = (
( "&", ";" )
);
};
}</pre>2. On the first line of your new text file, put an open parentheses: "(", then paste in the HTML definition on the next line. Close out the parentheses on the last line: ")".

3. In your new file, change the SupportsIndentation line to " YES" instead of " NO":

<pre class="command">SupportsIndentation = YES;</pre>4. Save your new file as " HTMLOverride.pblangspec" in this directory:

<pre class="command">/Library/Application Support/Apple/Developer Tools/Specifications</pre>5. Quit and restart Xcode, and you should now be able to auto-indent in HTML files...

charlie

Feb 10, 2007 12:15 PM in response to Charles Minow

Sweet! Now, when I choose the Javascript File type, the indenting works. Also, if I click on File>New Empty File and save with a .html extension, the indenting works. I'm not sure why Xcode doesn't apply indenting by default to any file opened in Xcode?? That would seem to be the natural thing to do.

I'm curious, why not change the HTML spec's indenting to YES in the original file? Why override it in another file?

Now it seems to me, it might also be possible to create a project type for Javascript, where when you click on build, the .html file launches in Safari. I'm going to search around on google.

Feb 10, 2007 12:57 PM in response to 7stud

I'm curious, why not change the HTML spec's indenting
to YES in the original file? Why override it in
another file?


Well, the main reason is that anything inside /System/Library/..., especially if it's in a Private Framework directory is, by custom, owned by Apple, and theirs to use and abuse as they see fit. So they might overwrite your changes at any time during a software update. I'm not sure about the other location, so it'd still be a good idea to back up that file, but it at least seems safer. You might also be able to set up a folder in your own user's ~/Library/Application Support folder, keeping the same directory structure below that, but I haven't tried that yet to see if it works.

charlie

Feb 10, 2007 2:31 PM in response to Charles Minow

Ok, thanks.

I'm having two other problems with Xcode:

1) I'm still having a problem with the exit messages for my C++ programs. To recap, the first hello world project I created exits with the message:

c++ test2 has exited with status 0.

But now all the C++ projects I create exit with the message:

«PROJECTNAME» has exited with status 0.

I'm using the project type: Command Line Tool/C++ Tool to create my C++ programs.

At this point, unless you can puzzle out the C++Tool.1 file, you might be best off to use Pacifist to extract the default template out of your Xcode installer package.c++

I looked at the generated theProjectName.1 file in my first C++ project and compared it side by side to the one generated in a subsequent project, and they are identical except for the generated dates and project names.

The only difference I can see between my first and subsequent projects is that under the project heading Executables, it lists the actual project name in my first project, where in subsequent projects it lists <<PROJECTNAME>>. There is also a project heading called Products, which has a Terminal icon next to it, and when I click on it, it executes the program in Terminal, and I notice the output in Terminal is slightly different:

c++ test2 (In Xcode exits with the message: c++ test2 has exited with status 0.)
Last login: Sat Feb 10 14:21:54 on ttyp1
Welcome to Darwin!
My-Names-computer:~ myUserName$ /Users/myUserName/Documents/xcode\ stuff/c++\ test2/build/Debug/c++\ test2; exit
hello
world
Hit Enter to Quit

logout
[Process completed]



c++ test3 (In Xcode exits with the message: «PROJECTNAME» has exited with status 0.)
Last login: Sat Feb 10 14:19:40 on ttyp2
/Users/myUserName/Documents/xcode\ stuff/c++\ test3/build/Debug/c++\ test3; exit
Welcome to Darwin!
My-Names-computer:~ myUserName$ /Users/myUserName/Documents/xcode\ stuff/c++\ test3/build/Debug/c++\ test3; exit
hello world
Hit Enter to Quit

logout
[Process completed]


It looks like with c++ test3 the command to run the program gets entered twice (and the first one is ignored?).

2) I also tried running a hello world program in Java (project type: Java/Java Tool). The first project I created worked fine. I didn't change a thing in the default program that was generated, which merely output hello world when I clicked on Build and Go. Now, when I create a new Java project, and I try to execute the default hello world program that is generated, I get the error message:

Unable to access jarfile «PROJECTNAMEASIDENTIFIER».jar
java has exited with status 1.


Apparently, I don't have a clue what I am doing in Xcode because every time I create a project, it messes up subsequent projects.


17" intelimac 2ghz 2gb 250gb Mac OS X (10.4.7)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Xcode and javascript/php/html development?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.