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

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 »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »

Price Scanner via MacPrices.net

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
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... 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
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
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.