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

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.