KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Exporting a method as text programmatically
PRODUCT: 4D | VERSION: 11.4 | PLATFORM: Mac & Win
Published On: May 21, 2009


Exporting a method body as text can be handled programmatically by using macros in 4D.
Here is a sample macro.xml file:

<?xml version="1.0" ecoding="UTF-8" standalone="no" ?>
<!DOCTYPE macros SYSTEM "https://www.4d.com/dtd/2007/macros.dtd">

<macros>
    <macro name="Method_text" method_event="on_save" version="2">
      <text>
          <method>MethodBody("<method_name><method_name />")</method>
      </text>
    </macro>
</macros>


In this example, the "method_event=on_save" attribute specifies when the project method named "MethodBody" is to be executed. The "on_save" attribute is triggered only trough the Save Method option in the File menu. To be accessible from 4D, this macro should be saved in the "Macros V2" folder.

The "MethodBody" method handles the capturing of the method's text and sending it to an external text file:

` MethodBody method

C_TEXT($methodName;$1)
C_TEXT($text)
C_TIME($vDocRef)

$methodName:=$1
GET MACRO PARAMETER(Full method text;$text)
$vDocRef:=Create document("")
If(OK=1)
   SEND PACKET($vDocRef;"Method Name: ")
   SEND PACKET($VDocRef; $methodName)
   SEND PACKET($vDocRef; Char(Carriage return))
   SEND PACKET($vDocRef; String(50*char(95)))
   SEND PACKET($vDocRef; String(4*Char(Carriage return)))
   SEND PACKET($vDocRef;$text)
   CLOSE DOCUMENT($vDocRef)
End if

Commented by Charles J. Miller on November 8, 2011 at 10:02 AM
THE MACRO IN V11 AT LEAST SHOULD READ AS FOLLOWS:



Method_text ("")


Commented by Registrierung Serviceaccount on November 15, 2010 at 6:00 AM
Ah - looks like I ran into the same issue as Bob, let's try again :

<method_name><method_name />

needs changing to

<method_name/>
Commented by John Milton on November 15, 2010 at 5:57 AM
NB Typo in the macro above : "n" missing from "encoding" in line 1

Also (in v12 at least) I had to change "" to "" to get it to work.

Other than that works well - a nice easy way to get methods automatically saved to text out of 4D - allowing them to be included in my git source control :-)
Commented by Bob Shurtleff on June 19, 2010 at 11:24 PM
The text of the macro:
MethodBody("")
lacks a tag. It should read:
MethodBody("")
With this addition, the macro and the associated method function correctly.
Commented by James Rowe on May 24, 2010 at 1:09 PM
Don't believe the syntax highlighting. Second "SEND PACKET" from the bottom of the if block has "Srting" misspelled. Notice the transposition of "r" and "t".
Commented by Atanas Atanassov on June 16, 2009 at 10:37 AM
To avoid errors in the macro code it is better to type the macro code into the text editor! Do not use copy and paste!
Commented by Jesse Pina on June 12, 2009 at 11:03 AM
This can be used to save different versions of methods, simply by adding a few things like a date stamp or id number to the file name and/or exported method content
Commented by Josh Fletcher on May 28, 2009 at 6:28 PM
For more information be sure to check out the Macros Tech Note too: http://kb.4d.com/search/assetid=47934