Create a Neat Apple Script MenuBar App

We can use Apple Script to create a simple menu bar app. What this menu bar app will actually do depends on individual needs. Below you can download a simple example script which has only the minimum code required to create a menu bar app. Download this one if you like to build your menu options from scratch.

You can also get the Controller script and it's support files. But to make everything work some tweaking is required.

Basic Menu Bar Script

This is the minimal script needed to create the menu bar app. You can start with this and mod it accordingly to your needs.

There are only three options/examples in this minimum script:

- Do Something will create a folder on your Desktop.
- Do Something Else will delete that created folder.
- Quit exists the app.

Download

Basic Menu Bar Script - https://www.mteam7.com/files/MenuBarApp.zip

Controller Script

MenubarAppI'll add everything you will need to make this script fully functional, as shown in the image. So if is this you want, let's go!

You'll need the following:

I'll remove the "Mount/Unmount Admin" options from the script because I believe no one will have use for it. As for the rest of the menu let's make it work!

Hosts Filter

(1) Install hosts files

Simple, just unpack the hosts.zip and move ONLY the hosts.clean and hosts.full files to your /etc directory.

(2) Install control script

If you like to do the automatic hosts block trick then you'll need to use the hosts file included, which is a bash script file I use to control the whole thing via terminal.

We need a permanent directory to store these support scripts in. I keep all my scripts in the directory /Users/My_User/bin, and that's a good path for these, I'll be using this path in our examples. So move the hosts script file into this /Users/My_User/bin directory.

Then make the script executable with:
cd /Users/My_User/bin
chmod +x hosts
Then execute the script without any flag to get some help:
 aegon [~/Bin] $ ./hosts

   Usage: hosts [option] [hostname] [ip]

   Options: -a  add a host
            -r  remove a host
            -u  updates the hosts template
            -b  block hosts
            -c  clean hosts
            -e  edit hosts

   Examples:
             hosts -u
             hosts -a testing.com
             hosts -r testing.com
             hosts -a testing.com 0.0.0.0
             hosts -r testing.com 0.0.0.0

   * No ip will default to 0.0.0.0
As you can see the command hosts -b will block all bad hosts, and hosts -c will remove the filters and revert to the default.

Finally, we need to edit the cronjob list so this "hosts -b" command will execute every X minutes. To do that set nano as our default terminal editor by opening a terminal and executing this:
export EDITOR='nano'
Now to edit the cronjob list type:
crontab -e
Paste the line below in the editor. This tells crontab to execute the "hosts -b" command every 15 minutes of every hour of every day of every month of every year:
*/15 * * * * /Users/My_User/bin/hosts -b
Don't forget to modify the name of your user in the path above.

To save the cronjob file press CTRL+O and exist the editor with CTRL+X. Now cron will execute this "/Users/My_User/bin/hosts -b" command every 15 minutes, effectively blocking the domains within the hosts.full file. Call me paranoid...

Final Considerations About the Hosts Filter

Keep in mind that when you use the hosts blocker there is a change some sites you need to connect are being blocked, if that's the case you should edit the hosts.full file and remove the line containing the sites you want to be able to connect to. Do not edit the /etc/hosts file, always edit the /etc/hosts.full file.

The reason for this is that the /etc/hosts file is overwritten when you block/unblock these hosts, so you should edit the template file, which is the /etc/hosts.full file and your changes will persist.

Wally Wallpaper Changer

We need two things to make this work, one is the wallpaper.scpt script file and the other are the 4 sets of wallpapers, each on its own folder.

(1) Prepare the Support Files

So extract the wallpaper.scpt.zip file and move the Wallpaper folder into your user's Pictures folder. You can copy your own wallpapers into these four folders later. For now the three samples inside each folder will do.

(2) Setup the Cronjob

Copy the wallpaper.scpt into that /Users/My_User/Bin folder we've created in the hosts setup step, then add the proper cronjob line, just like we did before. So type:
crontab -e
And paste the following after the hosts line we've added earlier:
*/8 * * * * osascript /Users/My_User/Bin/wallpaper.scpt
Again, change the My_User word with your user's correct path, and save the cronjob file with CTRL+O, exit nano with CTRL+X.

This will execute the script that changes the wallpaper every 8 minutes.

Edit Controller.scpt Variables

Before we save the script as an application we need to edit it with Script Editor and modify some variables. So double-click the Controller.scpt file to open it and change the following:

(1) Correct Time for Wallpaper Sets Change

Maybe you need to fix the correct time dusk, dawn, morning and night wallpapers are displayed. If so, consider the variable h as the current time. Then just follow the logic of the statements to fix the correct time things happen in your area.

So locate the following block:
 if (h > 8 and h < 12) then
        set timeNow to morning
    else if (h ≥ 12 and h < 17) then
        set timeNow to afternoon
    else if (h ≥ 17 and h < 18) then
        set timeNow to dawn
    else if (h ≥ 18 or h < 5) then
        set timeNow to night
    else if (h ≥ 5 or h < 8) then
        set timeNow to dusk
    end if
For the first two lines that logic would be:
if h(current time) is bigger than 8(am) and smaller than 12(noon) then
set timeNow to morning


Fix the other lines of this block as well, and try no to overlap times, in other words, where one time ends the other should begin. And at the end where dawn finishes is where morning should start, in the above block that is 8.

(2) Fix Bootcamp Variables

If you do not have a Bootcamp partition or just want to remove the Bootcamp options from the menu then skip to step 4, Removing Bootcamp Code.

Now, if you have a Bootcamp partition (Windows installed) do the steps below.

Find the information about the partition executing this terminal command:
  aegon [~/Desktop] $ diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                  Apple_HFS Sierra                  322.0 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
   4:       Microsoft Basic Data Dozer7                  130.3 GB   disk0s4
   5:           Linux Filesystem                         47.0 GB    disk0s5
As you can see the identifier (node) for my windows partition is disk0s4 and the name of the partition is Dozer7. Find the following line and change the name to match the name of your windows partition:
set mountedDiskName to "Bootcamp"
Now find all the lines containing the partition node below and change it to the node's identifier of your windows' partition:
/dev/disk0s4

(3) Prevent Windows From Auto-Mounting

If you want to prevent the Windows partition from auto mounting at boot you need to find out the UUID of the partition and then edit the /etc/fstab file.

To find out the UUID of your Windows partition type the command below, but change the node disk0s4, to match that of your Windows' partition.
diskutil info disk0s4 | egrep -om1 "([A-Z0-9]{8}.[A-Z0-9]{4}.+$)"
The result should be something like this:
  aegon [~/Desktop] $ diskutil info disk0s4 | egrep -om1 "([A-Z0-9]{8}.[A-Z0-9]{4}.+$)"
93F7F42E-B9B0-4AAB-8DD5-3A64B5CBBBDD
The 93F7F42E-B9B0-4AAB-8DD5-3A64B5CBBBDD is the UUID. Now edit the /etc/fstab file with nano by typing:
sudo nano /etc/fstab
And add the line below to the end of the file. Edit the UUID to match yours:
UUID=93F7F42E-B9B0-4AAB-8DD5-3A64B5CBBBDD none ntfs rw,noauto
That's it! Now save it with CTRL+O and exit nano with CTRL+X. Next time you boot your Mac the Windows partition will not auto-mount. You can mount it using the script's menu, by using Disk Utility or by using the good and old mount terminal command.

(4) Removing Bootcamp Code

If you do not have a Bootcamp to control (or simply don't care to control it in this manner) then the code that controls a Bootcamp partition is useless! But we can remove it.

Starting from the beginning of the file, find the set menuList to line and remove this from it:
"", "    Windows R/W", " Windows RO",
Note that I can't paste the emoji here, so remove everything from that "", to the RO",.

Remove also all these blocks below:
     set mountedDiskName to "Bootcamp"
        set diskIsMounted to false
     tell application "System Events" to set diskNames to name of every disk
        if mountedDiskName is in diskNames then
            set diskIsMounted to true
        end if
        
        if diskIsMounted then
            do shell script "diskutil unmount /dev/disk0s4"
        end if
 else if menuTitle is equal to "✏    Windows R/W" then
        itemStatus's setTitle:"✏"
        do shell script "mkdir -p ~/Desktop/Windows &> /dev/null &"
        try
            do shell script "diskutil unmount /dev/disk0s4" with administrator privileges
        end try
        try
            do shell script "mount -t ntfs -o rw,nobrowse /dev/disk0s4 ~/Desktop/Windows" with administrator privileges
        end try
        checkHost()
        
    else if menuTitle is equal to "✒    Windows RO" then
        itemStatus's setTitle:"✒"
        try
            do shell script "diskutil unmount /dev/disk0s4"
        end try
        try
            do shell script "diskutil mount /dev/disk0s4"
        end try
        
        checkHost()

(5) Fix Preferences Path

This is the final step before saving the script as an application. And whatever the name you plan to use for the app is what you need to change in this line. Mine is called Controller.app, so if you use this name there is no need to change anything. Otherwise, set the path and app name at the end of this line:
do shell script "open -a '/Applications/Utilities/Script Editor.app' /Applications/Controller.app"

Saving as Application

For now save it .app on your Desktop, we will move it to the /Applications folder later.

So click on File > Export....

In File Format choose Application also check the box Stay open after run handler then click on Save. This will save the Application.

Suppressing the Application's Icon From the Dock

There are two ways to do that, manually with a text editor or using Terminal and PlistBuddy.

Using PlistBuddy

Let's consider that the .app you've just saved is on the Desktop, and the name of the app is Controller.app. If thats the case, open Terminal and type:
/usr/libexec/PlistBuddy -c "Add :LSUIElement bool true" ~/Desktop/Controller.app/Contents/Info.plist

Manually Using a Text Editor

  1. Right click the app and click on Show package contents
  2. Open the Content folder
  3. Open the Info.plist file with a text editor. (Sublime, Text Edit, BBEdit, etc)
  4. Change the end of the file from this:
</dict>
</plist>
To this:

<key>LSUIElement</key>
<true/>
</dict>
</plist>
Save the .plist file and close it. that's it!

Now you can move the app to the /Applications folder and run it. You can also set the app to load at login in the System Preferences app under Users & Groups, Login Items tab.

Final Touches

Here is the icon I use for this app. To save it right click the image and choose save image.

Traficlights

To apply it do this:

  1. Open the image in Preview
  2. Select all the image by pressing CMD+A
  3. Copy it by pressing CMD+C
  4. Now select the app and press CMD+I
  5. In the Info window that will open select the Apple Script icon on the top
  6. Press CMD+V
And here are emojis in case you want to customize the menu's icons. Just remember to change all occurrences of the emoji icon you are changing.

{{ message }}

{{ 'Comments are closed.' | trans }}