How To: Use Very Basic AppleScript to Create a Fake Virus

Use Very Basic AppleScript to Create a Fake Virus

How to Use Very Basic AppleScript to Create a Fake Virus

AppleScript is quite an easy scripting language to use. In this guide, I will be showing you several commands. Near the end, I will show how these can be used to create a fake virus that actually does no harm.

Of course, this is only one way to use these commands—use your imagination and share anything you make in the comments. Those of you who read my last article should know what to expect.

This is quite a long article, but hopefully it'll be worth reading and you'll be scripting in no time.

The AppleScript Editor

The AppleScript editor is very easy to use. To open it, you can search for Script Editor. Alternatively, you can find the editor in the Applications folder or the Utilities folder in Launch Pad in newer versions of Mac OS. When you open the editor, this is what you should see:

  1. Record - This button is used to record exactly what you do. For example, if you clicked record, opened Safari and stopped recording, the editor would compose a script to open Safari. I don't mention this button any more in this tutorial, but it's pretty easy to use should you want to.
  2. Run/Stop - These are used to test the script you've compiled. These should be fairly self-explanatory.
  3. Compile - This button is used to compile your script. When you write the script with the editor, you must compile it before the script can be run or saved as an application. Compiling checks for errors and formats the script.
  4. Text Entry - This is where you write your script. You type normally in here.

The "Beep" Command

To start, type "beep" without the quotes in the field I've marked as number 4, click compile, and then click run. The computer should beep at you if the sound is on. The beep command is probably the simplest in AppleScript.

The "Say" Command

This command is used to make the computer speak to the user audibly. The basic use of this is as follows:

say "Hello World"

This command would make the computer say "Hello World" using the default voice. However, this can be changed to another voice. You do this by adding "Using "Voice"" to the end of the script. For example:

say "Hello World" using "Bruce"

This would make the computer say "Hello World" using Bruce's voice. You can see all of the voices available on your system like this:

  1. Open System Preferences.
  2. In the System section, click Speech.
  3. Click the "Text to Speech" tab.
  4. Look at the System Voice pop-up menu.

Try a script yourself. Tell the computer what you want it to say and what voice to use, click compile, and then run. You've now written your own AppleScript!

The "Display Dialog" Command

This command tells the computer to display a message. The basic use of this is:

display dialog "Hello World"

This would make the computer display a message saying "Hello World" with an OK button or a Cancel button to continue, like this:

You can change the buttons and choose the default button for this message by making this code slightly more complex. Here's an example that does both of these:

display dialog "Hello World" buttons {"Hello", "Bye"} default button 1

This would make the computer display a message saying "Hello World" with 2 buttons, one saying "Hello" and the other "Bye". When this dialog box first opens the first button ("Hello") will be highlighted. This is what the result of this script will look like:

Now you should understand the "say" and "display dialog" commands, which is enough to write your own small script, but I'm not done yet.

"If" Statements

"If" statements are the main things that stop a script executing in exactly the same way every time it's run. Sometimes you want this and sometimes not. "If" statements are particularly useful with the display dialog command. There are 2 parts to a simple "If" statement, "If" and "End if". These go around the code you want to execute if something is true. This is a simple example of an "if" statement using the returned button of a dialog:

display dialog "Hello World" buttons {"Hello", "Bye"} default button 1
if the button returned of the result is "Hello" then
say "Yay"
end if

This script makes the computer open a dialog box like the one at the end of the section on the "Display Dialog" command, and if the user clicks on the button saying hello then the computer will say "Yay" using the default voice. If not, the script should end.

This script is starting to look more complicated, but it's really not too bad is it? The next part of an "If" statement is "Else". This tells the computer what to do if the "If" statement isn't true. An else is optional and can be skipped. This is an example of how to use "Else":

display dialog "Hello World" buttons {"Hello", "Bye"} default button 1
if the button returned of the result is "Hello" then
say "Yay"
else
say "Aww"
end if

This would do the same as the last script, but instead of nothing happening if the user clicked "Bye" instead of "Hello", the computer should say "Aww" using the default voice. Now you have a simple script that doesn't have to result in the same thing every time it's run. This is how the script should look in the editor after compiling:

The "Else" can also be combined with another "if" to form an "Else if" statement. This "if" statement is only checked for if the first "If" statement is not true, but I'm not going into any more detail on "Else if" statements in this guide. "If" statements and the "Display dialog" command can be used together with nothing else to create a simple looking text-based game.

The "Set" Command

This section could go on forever, but I'm going to try to be brief. Like most commands in AppleScript, the "Set" command does exactly what you'd expect—sets a variable to what you want. The easiest example is the volume variable which on my Mac could range from 0 to 7. This is an example of how to use the set command:

Set Volume 7

Easy right? This would set my computers volume to 7 (Full), and I can use it as often as I'd like in a script.

The Fake Virus

This is the part that some of you will have been reading this article for. So far, you should know how to use:

  • The Beep Command
  • The Say Command
  • The Display Dialog Command
  • If Statements
  • The Set Command

This may not seem like much, but it's all you need to create an effective fake virus,. This is the code I used for mine. You can edit this in any way you would like, although I'm not responsible for any problems that occur as a result of this, and there are probably rules or laws against using this on other people's machines.

Anyway, here's my code. Ignore the blank lines because they keep being added by the WonderHowTo editor and I can't remove them; they shouldn't be there, but I doubt they'll have any effect on the code:

display dialog "A Virus Has Been Detected" buttons {"Destroy Virus", "Crash CPU (Risky)"} default button 1

if the button returned of the result is "Destroy Virus" then
display dialog "haha" buttons {"Oh No", "Ok"} default button 1
set volume 7
say "you cannot destroy me"
display dialog "trying attempt 2" buttons {"Oh No", "Ok"} default button 1
set volume 7
beep
display dialog "trying last attempt" buttons {"Oh No", "Ok"} default button 1
set volume 7
beep
say "Virus Upload Complete"
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "Spreading Virus" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "Spreading Virus" buttons {"NOOO", "Ok"} default button 2
set volume 7
beep
display dialog "Spreading Virus" buttons {"NOOO", "Ok"} default button 2
set volume 7
beep
display dialog "Spreading Virus" buttons {"NOOO", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "Spreading Virus" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "Spreading Virus" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "Spreading Virus" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2
set volume 7
beep

display dialog "Thanks For Your Help" buttons {"You're Welcome :)", "Damn You :@"} default button 1

else
display dialog "Good Move"
end if

Comments on the Fake Virus

  • The first dialog box gives you a chance to stop the fake virus before the computer starts beeping and showing errors. To remove this, delete the first display dialog command, the if statement, the "else" and everything after the "else".
  • This virus sets the volume to 7 and beeps on every message, meaning that it's obvious to everyone in the room that something's happening. If you don't want this, remove the "set" commands and the "beep" commands.
  • The virus could be shortened a lot by using the repeat command, but then the script wouldn't end without more complex coding. Read the section after the next one for more commands including the "Repeat" command.

Saving Your Scripts as Applications

This is quite simple and easy to work out. You do it like this:

  1. Open the file menu and click "Save As".
  2. Choose a name for your file.
  3. Open the dropdown menu of "File Format" and select "Application".
  4. Check or uncheck the startup screen, run only, and stay open boxes. If the "Startup Screen" box is checked, then before the application runs, the computer will display a dialog box that asks the user if they really want to run the application. The "Run Only" box means that the script can't be edited and the "Stay Open" box decides whether the application remains open after the script is finished. For my fake virus, I would have only the "Run Only" box checked.
  5. Click Save.

More AppleScript Commands

This section covers some other fairly simple AppleScript commands. The first command I'm going to talk about is the "tell" command, which is used to tell something else, normally another application to do something, like "Activate". Activate means that it will make the program you select the active program and bring the window to the front. The "tell" command is like the "If" command in the sense that it includes "Tell" and "End tell". The "tell" command is used like this:

tell application "Safari"
activate
end tell

This script would bring the Safari window to the front.

Another useful command is the "repeat" command. Again, this should be used with a start and end ("Repeat" and "End repeat"). The repeat command will repeat the code between the start and end until told to stop, normally with the "exit repeat" command, unless you tell the script to "repeat x times". Here's an example of how to use the repeat command in your fake virus, although this takes the script a step further as it can sometimes make a Mac almost impossible to use until the script or application is stopped, which is sometimes hard to do except by stopping the power going to the Mac by unplugging it or by holding the power button. Anyway, here's the script:

repeat
tell application "Safari"
activate
end tell
tell application "Preview"
activate
end tell
tell application "Finder"
activate
end tell
end repeat

This script will repeatedly change the active program very quickly, making it very difficult to save, type or read, and the repeat loop will not end until the script ends, which can only be done by quitting the script's application. However, because it wont stay active for long, this can be difficult (although the shortcut makes quitting slightly easier).

This page includes more detail on the repeat command.

The final thing I'm going to mention isn't a whole new command, but an addition to the "Display dialog" command that will add a text entry field as well as the buttons. To do this, you add "default answer """ to the end of the command, you can use this with an if statement to add a password that can stop the virus. Here's an example with a text entry field:

display dialog "Enter Password" buttons {"Ok", "Forgot?"} default button 1 default answer ""

If I were doing this at the start of my virus, then I would make the "forgot?" button run the virus as well as the wrong password.This page should help you if you want to add a working password box.

I hope that everyone who reads this will learn at least one thing they didn't know already. Get scripting and share your ideas in the comments. If you need any help, add a comment or look on the Internet—there are loads of resources on Google to help do anything you'd like with AppleScript.

7 Comments

Hah! This is a clever little prank. The only thing I would change is to make it say "All Your Base Are Belong To Us!" before the flood of dialog prompts.

Maybe add icons to some of the dialogs and instead of writing the same 10 times, use the repeat feature? =P
like:

--Main Code
display dialog "A Virus Has Been Detected" buttons {"Destroy Virus", "Crash CPU (Risky)"} default button 1 with icon 0

if the button returned of the result is "Destroy Virus" then
display dialog "haha" buttons {"Oh No", "Ok"} default button 1
set volume 7 with icon caution
say "you cannot destroy me"
display dialog "trying attempt 2" buttons {"Oh No", "Ok"} default button 1 with icon 1
set volume 7
beep
display dialog "trying last attempt" buttons {"Oh No", "Ok"} default button 1 with icon 0
set volume 7
beep
say "Virus Upload Complete"

--Reapeat
repeat 15 times
display dialog "@ERROR274520" buttons {"Help", "Ok"} default button 2 with icon 0
set volume 7
beep
end repeat

--Dialog
display dialog "Thanks For Your Help" buttons {"You're Welcome :)", "Damn You :@"} default button 1

else
display dialog "Good Move"
end if

You're right, this code would be a much simpler way of writing it, and I had never really looked into icons, so that's a nice addition, thanks.

I think that if you can name it something innocent thats on your machine and get the icon and change it, it would make it easier to spread. E.g. I named mine Steam and replaced the original icon with the Steam one :)

thank you. now i made a virus that spams the whole inventory of a standart computer. i can spam kids with viruses.

Share Your Thoughts

  • Hot
  • Latest