TweetFollow Us on Twitter

Finder Strings
Volume Number:9
Issue Number:4
Column Tag:Fun Hacks

Related Info: Finder Interface

Rändóm fiNdEr sgnirtS

A cute extension to help you with April Fools Day

By By Mike Scanlin, MacTech Magazine Regular Contributing Author

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Recently at a friend’s house I saw a clock that ticks backwards. The whole thing looks like a normal clock would when viewed in a mirror. I thought to myself “Not very useful, is it? Kind of difficult to figure out what time it is. What if everything was backwards: newspapers, controls on my car, file names in the Finder... <click> Hey! Cool idea for an April Fool’s INIT.” And, so, here we are.

RandomFinderStrings is an INIT (Extension) that causes one of several effects to happen to each string drawn by the Finder: (1) the case of the letters is forced to be upper, lower, reversed or random, (2) the letters in the string are rearranged back-to-front, (3) all vowels get an accent mark or (4) all vowels are removed. The effect that happens to each string is random. The framework of the code has been set up to make it easy to add your own effects if you can think of a cool way to tweak a Str255 (maybe you could do a conversion to Pig Latin or something) or, you can just use the code as it appears here. You can have some fun if you give this INIT an unsuspecting name like “Easy Access” and put it on a [former?] friend’s Mac.

HOW DOES IT WORK?

The first part of the INIT code creates space for the patch and effects code in the system heap (about 800 bytes) and moves a copy of the code there. Then it installs a head patch on DrawString. The patched DrawString will first check if CurApName == “\pFinder” and that the Option key is not down. If both conditions are true then it will randomly change a copy of the string before calling the real DrawString to draw the string. Note that comment about the Option key. If you really need to see the true strings once this INIT is installed, and you can’t afford to remove it and reboot, you can hold down the Option key and cause the screen to refresh (with a screen saver)-everything will redraw normally (but if you let go of the option key then things begin to get drawn tweaked again).

WHAT DOES IT LOOK LIKE?

Here are some screen shots that show some of the effects this INIT is capable of. Figure 1 shows the System Folder when ACCENTED_VOWELS is on, figure 2 shows the About This Macintosh box when BACKWARDS_LETTERS is on and, figure 3 shows the File menu when DO_CASE_CHANGES is on (the NO_VOWELS option isn’t shown here). You can mix and match these effects until you reach the level of annoyance you desire but you will have to recompile each time you change which effects are on/off. You turn them off by setting their #defined values to 0 and you turn them on by setting their #defined values to 1. If ALWAYS_TWEAK_STRING is off (i.e. zero) then not every string drawn by the Finder will be modified (because of the -1 case in the main switch statement).

Figure 1

Figure 2

Figure 3

Except for maybe the trap patching, this code is pretty easy to understand. The actual effects that tweak the copy of the string are rather simple. One thing to note might be the politically correct way to check if the Option key is down at a time when we don’t have an EventRecord: We make a dummy event record and call OSEventAvail with a mask of zero. This is a much better method than using GetKeys.

Because this is an INIT, the Think C Project Type should be “Code Resource” and the File Type should be “INIT”. You can use whatever you want for the Creator and ID (I used “Twen” with an ID of 55). Have fun.

/*************************************************
 * RandomFinderStrings.c
 *
 * This INIT installs a patch on DrawString that will tweak 
 * strings drawn by the Finder. If CurApName != “/pFinder” 
 * or if the option key is down then a normal DrawString 
 * takes place. If we are in the Finder then a modified 
 * version of the string is drawn: the upper/lower case of 
 * the letters might be changed, the string might be flipped 
 * backwards, all the vowels might get accent marks, etc.
 *
 * You can add to the list of effects by adding an item to 
 * the Effects enum and the main switch statement in 
 * MyDrawString. If your effects need globals, put them in 
 * the PatchGlobals struct and initialize them in main.
 *
 * In Think C, set the Project Type to Code Resource, the 
 * File Type to INIT, the Creator to anything, the Type to 
 * INIT, the ID to something like 55 (55 will work but it
 * doesn’t have to be 55), turn Custom Header ON and Attrs 
 * to 20 (purgeable) and Multi Segment OFF.
 *
 * Mike Scanlin. 24 Jan 1993.
 ***********************************************
 * defines
 ************************************************/
#define _DrawString         0xA884

enum Effects {
    UpperCase = 0,
    LowerCase,
    ReverseCase,
    RandomCase,
    BackwardsLetters,
    AccentedVowels,
    NoVowels,
    PigLatin,
    NumEffects /* must be last */
};

/* Turn these all on for maximum randomness */
#define DO_CASE_CHANGES     1
#define BACKWARDS_LETTERS   1
#define ACCENTED_VOWELS     1
#define NO_VOWELS           1
#define PIG_LATIN           0

#define ALWAYS_TWEAK_STRING 1

#if ALWAYS_TWEAK_STRING && !(DO_CASE_CHANGES || \
    BACKWARDS_LETTERS || ACCENTED_VOWELS || NO_VOWELS)
    not!
    /* Generate a compile time error if you set up an 
     * impossible situation. You need to have at least one 
     * effect enabled if ALWAYS_TWEAK_STRING is enabled. */
#endif

/*************************************************
 * typedefs
 ************************************************/
typedef pascal void (*DSProcPtr)(Str255 *theStringPtr);

typedef struct PatchGlobals {
    DSProcPtr       pgOldDS;
} PatchGlobals, *PatchGlobalsPtr;

/*************************************************
 * prototypes
 ************************************************/
void main(void);
void StartPatchCode(void);
pascal void MyDrawString(Str255 *theStringPtr);
short abs(short n);
void EndPatchCode(void);

/******************** main ***********************
 * Gets some memory in the system heap and installs the 
 * DrawString patch (as well as allocating and initializing 
 * the patch globals).  This is the only routine that gets 
 * executed at startup time (by the INIT mechanism).
 *
 * The block of memory that main allocates will
 * look like this when main has finished:
 *
 *                   +--------------------+
 *                   |    PatchGlobals    |
 *                   +--------------------+
 *                   |  StartPatchCode()  |
 *   DS trap addr -> +--------------------+
 *                   |   MyDrawString()   |
 *                   +--------------------+
 *                   |       abs()        |
 *                   +--------------------+
 *                   |   EndPatchCode()   |
 *                   +--------------------+
 ************************************************/
void main()
{
    Ptr             patchPtr;
    PatchGlobalsPtr pgPtr;
    long            codeSize, offset;
        
    /* try and get some memory in the system heap
     * for code and globals */
    codeSize = (long) EndPatchCode -
        (long) StartPatchCode;
    patchPtr = NewPtrSys(codeSize +
        sizeof(PatchGlobals));
    if (!patchPtr)
        return; /* out of memory-abort patching */
    
    /* initialize the patch globals at the
     * beginning of the block */
    pgPtr = (PatchGlobalsPtr) patchPtr;
    pgPtr->pgOldDS = (DSProcPtr)
        GetTrapAddress(_DrawString);
    
    /* move the code into place after the globals */
    BlockMove(StartPatchCode, patchPtr +
        sizeof(PatchGlobals), codeSize);
    
    /* set the patches */
    patchPtr += sizeof(PatchGlobals);
    offset = (long) MyDrawString -
        (long) StartPatchCode;
    SetTrapAddress((long) patchPtr + offset,
        _DrawString);
}

/***************** StartPatchCode ****************
 * Dummy proc to mark the beginning of the code 
 * for the patches. Make sure all of your patch
 * code is between here and EndPatchCode.
 ************************************************/
void StartPatchCode()
{
}

/*********************** MyDrawString **********************
 * Head patch on DrawString that randomly tweaks the string 
 * to be drawn if the current application is the Finder and 
 * the Option key is not down.
***********************************************************/
pascal void MyDrawString(Str255 *theStringPtr)
{
    Str255          copyOfString;
    EventRecord     theEvent;
    PatchGlobalsPtr pgPtr;
    Byte            *p, *inputPtr;
    short           rand;
    Byte            i, ch;
        
    /* if we’re not in the Finder then exit */
    if (*(char *) &CurApName != 6 ||
      *(long *) ((Ptr) &CurApName + 1) != ‘Find’ ||
      *(short *) ((Ptr) &CurApName + 5) != ‘er’ )
        goto DontDoAnything;
    
    /* if the option key is down then exit */
    OSEventAvail(0, &theEvent);
    if (theEvent.modifiers & optionKey)
        goto DontDoAnything;
    
    /* we’re in the Finder so do something to a
     * copy of the string (unless it’s zero-length) */
    if (i = *(Byte *) theStringPtr) {
        p = (Byte *) &copyOfString;
        BlockMove(theStringPtr, p++, i + 1);

TryAgain:

        switch (abs(Random()) % NumEffects) {
        
#if DO_CASE_CHANGES
        case UpperCase:
            /* force all letters to upper case */
            do {
                if (*p >= ‘a’ && *p <= ‘z’)
                    *p -= ‘a’ - ‘A’;
                p++;
            } while (-i);
            break;
            
        case LowerCase:
            /* force all letters to lower case */
            do {
                if (*p >= ‘A’ && *p <= ‘Z’)
                    *p += ‘a’ - ‘A’;
                p++;
            } while (-i);
            break;
            
        case ReverseCase:
            /* flip the upper/lower case of each letter */
            do {
                if (*p >= ‘a’ && *p <= ‘z’)
                    *p -= ‘a’ - ‘A’;
                else if (*p >= ‘A’ && *p <= ‘Z’)
                    *p += ‘a’ - ‘A’;
                p++;
            } while (-i);
            break;
            
        case RandomCase:
            /* randomly set the case of each letter */
            do {
                /* force to lower case */
                if (*p >= ‘A’ && *p <= ‘Z’)
                    *p += ‘a’ - ‘A’;
                /* make half of them upper case */
                if (*p >= ‘a’ && *p <= ‘z’ && Random() < 0)
                    *p -= ‘a’ - ‘A’;
                p++;
            } while (-i);
            break;
#endif

#if BACKWARDS_LETTERS
        case BackwardsLetters:
            /* flip the string front to back */
            inputPtr = (Byte *) theStringPtr + i;
            do {
                *p++ = *inputPtr;
                inputPtr-;
            } while (-i);
            break;
#endif

#if ACCENTED_VOWELS
        case AccentedVowels:
            /* put an accent mark over each vowel */
            do {
                rand = abs(Random());
                switch (*p) {
                case ‘A’:
                    switch (rand & (4-1)) {
                    case 0: *p = 0x80; break;
                    case 1: *p = 0x81; break;
                    case 2: *p = 0xCB; break;
                    case 3: *p = 0xCC; break;
                    }
                    break;
                case ‘E’:
                    *p = 0x83;
                    break;
                case ‘O’:
                    if (Random() < 0)
                        *p = 0x85;
                    else
                        *p = 0xCD;
                    break;
                case ‘U’:
                    *p = 0x86;
                    break;
                case ‘a’:
                    *p = 0x87 + (rand % 6);
                    break;
                case ‘e’:
                    *p = 0x8E + (rand & (4-1));
                    break;
                case ‘i’:
                    *p = 0x92 + (rand & (4-1));
                    break;
                case ‘o’:
                    *p = 0x97 + (rand % 5);
                    break;
                case ‘u’:
                    *p = 0x9C + (rand & (4-1));
                    break;
                default:
                    break;
                }
                p++;
            } while (-i);
            break;
#endif

#if NO_VOWELS
        case NoVowels:
            /* remove all vowels */
            inputPtr = (Byte *) theStringPtr + 1;
            do {
                ch = *inputPtr;
                if (ch >= ‘A’ && ch <= ‘Z’)
                    ch += ‘a’ - ‘A’;
                if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’
                  || ch == ‘o’ || ch == ‘u’)
                    copyOfString[0]-;
                else
                    *p++ = *inputPtr;
                inputPtr++;
            } while (-i);
            /* make sure the string length is at least 1 */
            if (copyOfString[0] == 0) {
                copyOfString[0] = 1;
                copyOfString[1] = 
 *((Byte *) theStringPtr + 1);
            }
            break;
#endif

#if PIG_LATIN
        case PigLatin:
            /* not implemented */
            break;
#endif

        case -1:                    
        default:
#if ALWAYS_TWEAK_STRING
            goto TryAgain;
#endif
            break;
        }
        
        theStringPtr = &copyOfString;
    }

DontDoAnything:
    
    /* find our globals */
    pgPtr = (PatchGlobalsPtr) ((long) StartPatchCode -
        sizeof(PatchGlobals));

    /* call the real DrawString */
    (*pgPtr->pgOldDS)(theStringPtr);
}

/********************* abs **********************
 * Return the absolute value of n.
 ************************************************/
short
abs(short n)
{
    if (n < 0)
        return (-n);
    return (n);
}


/******************* EndPatchCode ********************
 * Dummy proc to mark the end of the code for
 * the patches. Make sure all of your patch code
 * is between here and StartPatchCode.
 ************************************************/
void EndPatchCode()
{
}

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.