#!/usr/bin/perl # # genSetVarWithCoderPB.pl # v1.2 # This simple script generate code for the ``initWithCoder:'' method of # an Objective-C class. # # - Typical installation on Mac OS X: # Install this script in ~/Library/Scripts. # - Typical usage on Mac OS X: # Copy in the clipboard some text containing the member variable declarations # you wish to decode. Then select this script from the script menu at the top # right of the screen (search the internet for how to install the script menu). # Then simply do a "paste" somewhere in your code or text file and you will # see the generated code. If you don't want to use the Script menu, # you can also invoke the script from the terminal. # # Works with every type of variable; e.g., for the following variables: # NSString *name; # float expectedRaise; # the generated code is: # [self setName: [coder decodeObjectForKey:@"Name"]]; # [self setExpectedRaise: [coder decodeFloatForKey: @"expectedRaise"]]; # # Requirements: # - each member variable declaration must be correct Objective-C syntax; # - each member variable should occupy one and only one line; # - class names can begin with lowercase as well as uppercase (or _) chars; # - variable names can begin with lowercase as well as uppercase (or _) chars; # - empty lines between member variables are ok; # - IBOutlet and IBAction macros are not handled. # # Known bugs: # - for unknown reasons, activating this script from the Script menu doesn't # seem to work on Tiger. It does work on Panther, as well as activating it # from the command line. # # Change log: # v1.0 (20041118) Initial version # v1.1 (20050221) Corrected bug for `decodeBool' method # v1.2 (20051005) Removed evil \r characters from the input string. # # Created by Ettore Pasquini on 11/18/04. # Copyright 2004, 2005 Cubelogic. # This code is released under the GNU GPL without any warranty, without even the # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See # the GNU General Public License at http://gnu.org for more details. # Writing these notes took me much more than writing the script. # Tested on Mac OS X 10.3.6. # Report bugs to support@cubelogic.org $input = `/usr/bin/pbpaste`; # remove evil \r characters $_ = $input; s#\015\n#\n#g; s#\015#\n#g; $input = $_; @inputlist = split /\n/, $input; $output = ""; foreach $line (@inputlist) { if ($line =~ /^\s*id\s+/) { # it's an `id' object $line =~ s%^\s*id\s+(\w+);.*%\t[self set\u$1: [coder decodeObjectForKey:@"$1"]];%; } elsif ($line =~ /^\s*\w+\s*\*\s*\w+/) { # it's another object (XYZtype *) $line =~ s%^\s*\w+\s*\*\s*(\w+);.*%\t[self set\u$1: [coder decodeObjectForKey:@"$1"]];%; } elsif ($line =~ /^\s*BOOL\s+\w+/) { # it's a BOOL $line =~ s%^\s*(\w+)\s*(\w+);.*%\t[self set\u$2: [coder decodeBoolForKey:@"$2"]];%; } else { # it's a int, or float, or other primitive type $line =~ s%^\s*(\w+)\s*(\w+);.*%\t[self set\u$2: [coder decode\u$1ForKey:@"$2"]];%; } $output .= "$line\n"; } # open pasteboard as output file open(PBCOPY, "|-", '/usr/bin/pbcopy') or die "Unable to open pbcopy"; print PBCOPY $output; close PBCOPY;