Search and Replace in SourceCode using IDE Script

Is it possible to do a search and replace among the whole source code of my Projects, by using XojoScript?

Don’t know about XojoScript, but why not simply use the current Command-F or Ctrl-F, then enter the string to search and the string that replaces it, then click “All” ?

It replaces all occurrences in the whole project.

That said, I do not think you can do anything to the source with XojoScript, which is meant for the compiled application run.

You may be able to manipulate the source of your project with IDE Script (Build step). See :
http://documentation.xojo.com/index.php/IDE_Script

You can in an IDE Script, but really only the method you are currently looking at. You might be able to look at the source of other methods (I haven’t tested), but not in a convenient way, and I’d guess it would be sort of slow anyway.

Sascha, what are you trying to do?

This discussion made me try to do a search and replace among the whole source code.
So one could make a list within a script to look for own serials and passwords and replace them with obfuscated strings.

See Eric’s posts towards the end of that thread. Perhaps he’ll be willing to share his code.

Thank you @Michel Bujardet and @Kem Tekinay.

Because this seems to be a very rare usecase, i think Kem is right and i will just wait and see what the mentioned Thread will bring us (the community). :slight_smile:

It is possible through IDE Script to call a shell, so I tried to write an AppleScript to automate the replace command of the IDE. Problem is, the IDE takes command-f, then the keystrokes to fill both fields, then there is no way to use a keystroke to do the Replace All. A click on the ‘All’ button is necessary.

I could use an external utility to simulate the click, but if the IDE window is resized, it will no longer work.

So in conclusion, yes, it is possible to automate find/replace through IDE Script, with the help of AppleScript and MouseTools.

However, it is rather cumbersome.

Would be much easier using KeyboardMaestro. But i really wanted to do this only using the IDE Scripts. For compatibility reasons and to avoid someone to have to buy a Tool like KeyboardMaestro.

The IDE Script language is awfully limited. It will not permit replace of all occurrences, as it is necessary to specify each instance property.

AppleScript plus MouseTools or KeyboardMaestro for Mac, or Sendkeys plus a mouse click utility for PC can do the trick, but as I said, the click is reliable only if the IDE window is not resized.

A possible solution is to work on a saved XML version of the project, instead of trying to work in the IDE. IDE Script is too limited to even allow selection of the project format upon save, but a preprocessing tool seems possible, as XML is a text format. An app can laod that, do the replaces/crypting, and then the project can be reloaded and built with the obfuscated strings.

Just created a small AppleScript program completed by a small mouse click utility to automate a global string replace within a project. This can be called by a DoShellScript(string) from IDE Script.

Mouse Click utility clicks any area of the screen by its x and y coordinates I got from http://hints.macworld.com/article.php?story=2008051406323031 .
Download here.

Extract the unix file Click and copy to the root of Macintosh HD. You will need to provide your password to do so.

Script :

tell application "Xojo" activate end tell tell application "System Events" tell process "Xojo" -- Maximize keystroke "/" using {command down, shift down} keystroke (ASCII character 28) keystroke "z" keystroke (ASCII character 13) delay 1 -- Replace all keystroke "f" using {command down} keystroke "Untitled" keystroke {tab} keystroke "changed-" delay 1 keystroke (ASCII character 8) do shell script "/click -x 1857 -y 701" end tell end tell -- 8 backspace -- 28 for left -- 29 for right -- 30 for up -- 31 for down

In order to always be able to locate the “All” button to replace, the Xojo window has to be maximized, which is accomplished through triggering the Window menu and selecting ‘Zoom’. Problem is, Xojo does not always display ‘Zoom’ in that menu and replaces it by ‘Restore’. I have not found an immediate AppleScript workaround, but maximizing the IDE before running the script does the trick.

Thank you @Michel Bujardet, but you already provided a solution by recommending to do it within an XML version of the project.

And the above AppleScript solution has too many drawbacks (Mac only, needs controls at specific positions on the screen,…). If i really would do this on a Mac, KeyboardMaestro would make it much more reliable. :wink:

Be sure to note this thread too, in case it helps.

[quote=92946:@Sascha S]Thank you @Michel Bujardet, but you already provided a solution by recommending to do it within an XML version of the project.

And the above AppleScript solution has too many drawbacks (Mac only, needs controls at specific positions on the screen,…). If i really would do this on a Mac, KeyboardMaestro would make it much more reliable. ;)[/quote]

The XML approach is no doubt simpler. I just wanted to see what could be done with AppleScript and click. Incidentally, with Mavericks the command Click went the way side, and testing replacement is not a waste of time. Why Apple decided to remove a key component of its scripting language is one of those mysteries one can wonder about.

As for Keyboard Maestro, it is no doubt a powerful solution, but I like to do things without commercial solutions.

Furthermore, KeyboardMaestro is Mac only, and a PC solution is of interest.

Being able to script the iDE beyond the capabilities of IDE Script could be applied to much, much more than a simple replacement in the source.

About the Applescript: Shouldn’t it be possible to use the Accessibility API in AppleScript? That lets you find buttons and menu items by their caption, so no need for clicks into the blind. Has anyone tried?

Oh, and another shameless plug: Arbed can be scripted to perform global search and replace in projects. Arbed includes several example scripts for doing that, iterating over every item in the project and deciding how to change it as you like. However, using that feature is not free (since you probably want to save the changed project, you need the $59 license). Just saying.

I could not find any mention an Accessibility API access for AppleScript. So it would probably require creating an XCode program to access it. I am not knowledgeable enough to do so, but have no doubt someone can come up with that.

I have. You can access some things, like the find and replace fields, but not others, like the All button or the source editor.

I tested through System Events. Or did you mean something else?