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

The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »

Price Scanner via MacPrices.net

Apple is offering significant discounts on 16...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more
Apple HomePods on sale for $30-$50 off MSRP t...
Best Buy is offering a $30-$50 discount on Apple HomePods this weekend on their online store. The HomePod mini is on sale for $69.99, $30 off MSRP, while Best Buy has the full-size HomePod on sale... Read more
Limited-time sale: 13-inch M3 MacBook Airs fo...
Amazon has the base 13″ M3 MacBook Air (8GB/256GB) in stock and on sale for a limited time for $989 shipped. That’s $110 off MSRP, and it’s the lowest price we’ve seen so far for an M3-powered... Read more
13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more

Jobs Board

DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Operating Room Assistant - *Apple* Hill Sur...
Operating Room Assistant - Apple Hill Surgical Center - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.