Here is a script to build projects from command line - OSX

OK, there has been a lot of discussion about building projects from the command line. I whipped up a little script that will do just that, but it works on OS X only, sorry Windows/Linux guys… Bear with me, I had to hack the Apple Script because for some reason click menu item “blah” of menu item “blah” wasn’t working for me on sub menus, maybe some AppleScript Guru’s can solve that problem?

Anyway, first what you need to do is create a new IDE script containing:

[code]Dim projectToOpen As String = Clipboard

OpenFile(projectToOpen)

DoCommand “BuildApp”
DoCommand “CloseWindow”
[/code]

You want to name this so that it will always be at the top of your IDE Scripts menu because of my silly menu hacks. I named mine “1 - Build Script from Command Line”

Now, create a shell script, I named mine: xojo_build

[code]#!/bin/sh

echo “Building $1”

osascript -e "set the clipboard to “$1"”

osascript -e ’
tell application “Xojo” to activate

tell application “System Events”
tell process “Xojo”
tell menu bar 1
tell menu 3
click
key code 125
key code 125
key code 125
key code 125
key code 125
key code 125
key code 125
key code 125
key code 125
key code 125
key code 125
key code 124
key code 125
keystroke return
end tell
end tell
end tell
end tell’
[/code]

You can then compile from the command line:

$ ./xojo_compile /Users/jeremy/Projects/MyXojoProject/MyXojoProject.xojo_project

What this does…

  1. Sets the clipboard contents to the project you wish to open
  2. Clicks the File Menu
  3. Down arrow key 11 times to the IDE Scripts menu (hack!)
  4. Right arrow key 1 time to go into the IDE Scripts menu (hack!)
  5. Down arrow key 1 time to go to the 1st item inour IDE Scripts menu which should be “1 - Build Script from Command Line”

The Build Script from the command line:

  1. Opens the project as stated from the command line
  2. Calls the command DoBuild to build the project. I do this instead of using the command BuildApp so that it will build the application and platforms you previously had set. You could modify this to use BuildApp if you’d like
  3. Closes the project

One could modify the clipboard set/get commands to include additional information, such as:

[code]Xojo -project -analyse -outputfile

Xojo -project -compile -rundebug

Xojo -project -compile -runremote

Xojo -project -buildTargets[/code]

Parsing the command line would be a trivial addition if you made your command line arguments simple like above.

You can get the above code and others from my git repo: https://github.com/jcowgar/xojo-ide-scripts-misc, also be sure to checkout the Xojo Format Code script which has its own repo: https://github.com/jcowgar/xojo-format-code

The IDE has a much cleaner and cross platform way of doing this.

On OS X and Linux, the IDE has an IPCSocket (aka UNIX domain socket) that programs can connect to. Once connected, they can send the script contents over the socket and then either disconnect or send a NUL to have the script execute.

Here are the places the IPC socket may be:

  • /tmp/REALStudioIDE
  • /var/tmp/REALStudioIDE
  • $TMPDIR/TemporaryItems/REALStudioIDE (or whatever SpecialFolder.Temporary ends up returning)
  • ~/REALStudioIDE

On Windows, you have to connect via a Xojo IPCSocket to one of the listed paths. Other languages will not be able to access it.

An alternate approach is to use AppleScript to start the build. The IDE accepts the RBae/scpt event with the direct parameter being the script source. Since the IDE has no scripting dictionary, you either need to do it programmatically or use AppleScript’s raw syntax. For example:

tell application "Xojo" «event RBaescpt» "beep" end tell

Unfortunately there’s a bug in the event handler that makes it return errAEEventNotHandled ( -1708), even though it actually did handle it.

Joe, Very cool, however, I could not get Xojo on OS X to use RBIDEScript. I could not find REALStudioIDE or Xojo anything in any of the directories you mentioned. Does this work on OS X?

You can do this without the “arrow” hacks, and entirely from an AppleScript (those will run as shell scripts as of some past version of OS X).

Try something like this for your shell script:

#!/usr/bin/osascript

set scriptName to "1 - Build Script from Command Line.xojo_script"

tell application id "com.xojo.xojo" to activate

tell application "System Events"
	tell process "Xojo"
		set mb to menu bar 1
		set fileMenu to menu "File" of mb
		set IDEScriptsMenu to menu 1 of menu item "IDE Scripts" of fileMenu
		set scriptItem to menu item scriptName of IDEScriptsMenu
		click scriptItem
	end tell
end tell

Note that I use the app ID, not name, for the “activate” statement. This is safer as people (like me) will often rename their apps. (Mine is named “Xojo 2013r1” as I keep multiple versions as they come out.) System Events can access and manipulate UI elements directly, but only if “Enable access for assistive devices” is turned on in System Prefs.

Note too that I wrote and tested this before Joe’s comments.

Certainly should.
Start the IDE and then in terminal do

ls /tmp

[quote=14594:@Norman Palardy]Certainly should.
Start the IDE and then in terminal do

ls /tmp

It’s not there and Xojo is open.

$ ls /tmp CrashReportCopyLock-Jeremy's iPad launch-mxjYrM launchd-324.raRQPF CrashReportCopyLock-Jeremy's iPhone 5 launch-ogHDUN launchd-332.AslyRc geColladaModelCacheLock launch-vlAVou launchd-515.EjRERA geIconCacheLock launchd-2170.QxIiUu oobelib.log launch-aOXjS9 launchd-312.P4Gdrn xojofeedback

I guess I just assumed that since RBIDEScript wasn’t in Xojo and that I couldn’t find a socket anywhere that it had been taken out of Xojo.

The other approaches don’t require accessibility turned on. They also won’t break if we rename any menu item.

[quote=14592:@Kem Tekinay]Try something like this for your shell script:

set IDEScriptsMenu to menu 1 of menu item "IDE Scripts" of fileMenu [/quote]

That is what I was missing… menu 1 of menu item… I was using menu item “1 - blah” of menu item “IDE Scripts” … AppleScript was always fighting with me. Didn’t even dawn on me I had to get the sub-menu (menu 1) of IDE Scripts"

I love to use that method, but where is my socket? I compiled rbidescript from my old version of RB:

$ ./rbidescript -c Socket error: can't connect rbidescript:IDE Communication Path:

Also see my previous output of ls /tmp

$ ls /private/tmp/ CrashReportCopyLock-Jeremy's iPad launch-mxjYrM launchd-324.raRQPF CrashReportCopyLock-Jeremy's iPhone 5 launch-ogHDUN launchd-332.AslyRc geColladaModelCacheLock launch-vlAVou launchd-515.EjRERA geIconCacheLock launchd-2170.QxIiUu oobelib.log launch-aOXjS9 launchd-312.P4Gdrn xojofeedback

/tmp is just a link to /private/tmp:

$ ls -l / | grep tmp lrwxr-xr-x@ 1 root wheel 11 Aug 27 2012 tmp -> private/tmp

OK, I restarted Xojo… It now appears in /tmp… I rarely close Xojo, maybe it is getting closed somehow?

it should only close when you quit the IDE
if there’s some kind of error it might close & reopen

but you can just put the entire script IN your applescript & send it to the IDE instead of trying to script pressing the menus & selecting the right one (which is likely to be more error prone as it relies on the position of things not changing)

Hmmm … I notice the IDE Scripting sources are not present in the dmgs etc.
If you happen to have an old version of he IDE then look in Extras > IDE Scripting and there’s source for a tool you could turn into a command line app with parameters to do all this instead of trying to wire together applescripts & IDE scripts etc.

In the interest of completeness, here is my corrected version of my version of the shell script:

#!/usr/bin/osascript

on run argv
	
	set projectPath to item 1 of argv
	set the clipboard to projectPath
	
	set scriptName to "1 - Build Script from Command Line.xojo_script"
	
	tell application id "com.xojo.xojo" to activate
	
	tell application "System Events"
		tell process "Xojo"
			set mb to menu bar 1
			set fileMenu to menu "File" of mb
			set IDEScriptsMenu to menu 1 of menu item "IDE Scripts" of fileMenu
			set scriptItem to menu item scriptName of IDEScriptsMenu
			click scriptItem
		end tell
	end tell
	
	return "Finished building " & projectPath
end run

[quote=14589:@Joe Ranieri]On OS X and Linux, the IDE has an IPCSocket (aka UNIX domain socket) that programs can connect to. Once connected, they can send the script contents over the socket and then either disconnect or send a NUL to have the script execute.

[/quote]

I’m just now getting around to playing with this, but I have run into an issue - just wanted to see if I am doing something wrong, or if a feedback report is in order…

If I send the script, then send a ChrB(0), the IDE executes the script, but then stops listening (the file /tmp/XojoIDE disappears). Relaunching the IDE starts it listening again.

Is sending a ChrB(0) after sending the script what you meant by sending a NUL or did I misinterpret?

My tests are with Xojo 2013R3 (also 2013R3.3) on Mac OS X 10.8.5) - I haven’t had a chance to test on another machine yet.