Blog

Simple AppleScript app for clean Xcode

Clean temporary and unwanted files from Xcode with your own AppleScript app.

Recently my colleague had a problem with almost full space storage on his MacBook Air.

„Why again?” he was crying. „What takes so much disk space?”. It took me only a few minutes to realize who (what) was to blame - Xcode of course. I decided to help my friend, so I searched for help on the Internet. There were plenty of tutorials, how to clean temporary and not necessary files from Xcode. So we did it.

"But there must be an easier way!" I was thinking. And apparently, there is. Instead of manually deleting files from Finder, we can automatize the whole process. The best way to do it on macOS is via Automator app. Easy to use, even if you know little or nothing about programming. With Automator you can make a simple events queue for delete temporary files that are made from Xcode.

But this post won’t be about it ;)

During learning about Automator functions I realized that much easier (and more fun) will be writing simmilar app that will do the same. In a nicer way, of course. So I started with AppleScript, a very simple programming language made by Apple. Almost everything about this language you can find in Wikipedia or on Apple pages - with JavaScript examples.

I wanted a very simple script that will open six folders in Finder and remove files (with subfolders) from those to Trash. Let add to it an option of choosing what directories you want to find and confirmation alert. A piece of cake ;)

So, we need to start a new document - in Automator („Run AppleScript” function), or in AppleScript Editor. Both apps are system tools, so finding them in macOS won’t be a problem. Automator has a little advantage, because you can easily test the app during the coding process to check if it works.

First, I needed a list with directories. User will have a chance to choose directories from where he wants to delete files. So I created a simple „Choose from list” function:

set foldersList to {"DerivedData", "Archives", "iOS DeviceSupport", "CoreSimulator", "com.apple.dt.Xcode", "Backup"}
    set choseFoldersToDelete to (choose from list foldersList with title "Pick Xcode folders to delete" with prompt "Automator will remove files from selected folders to Trash" default items "None" OK button name "Next" cancel button name "Cancel" with multiple selections allowed)
    if choseFoldersToDelete is false then error number -128

As you can see, I created a simple foldersList with folders Names and then an alert to show it. „Choose from list” function has only two default buttons - OK and cancel. In my app, if cancel is picked, app will simply close itself.

The next thing is a confirmation alert that shows folders we’ve picked:

set theAlertText to "You've picked files from folders:"
    set saveTID to text item delimiters
    set text item delimiters to ", "
    set theAlertMessage to choseFoldersToDelete as text
    set text item delimiters to saveTID
    display alert theAlertText message theAlertMessage as critical buttons {"Cancel", "Delete files!"} default button "Delete files!" cancel button "Cancel"
    if theAlertText is false then error number -128

For directories that we’ve picked we need to create a queue that will move items and subfolders to the Trash. It’s easy, but I must say, a little more complicated than JavaScript:

tell application "Finder"
    repeat with a from 1 to length of choseFoldersToDelete
        set pickedItem to item a of choseFoldersToDelete
        if pickedItem is "DerivedData" then move every item of "~/Library/Developer/Xcode/DerivedData" to trash
        if pickedItem is "Archives" then move every item of "~/Library/Developer/Xcode/Archives" to trash
        if pickedItem is "iOS DeviceSupport" then move every item of "~/Library/Developer/Xcode/iOS DeviceSupport" to trash
        if pickedItem is "CoreSimulator" then move every item of "~/Library/Developer/CoreSimulator" to trash
        if pickedItem is "com.apple.dt.Xcode" then move every item of "~/Library/Caches/com.apple.dt.Xcode" to trash
        if pickedItem is "Backup" then move every item of "~/Library/Application Support/MobileSync/Backup" to trash
    end repeat

Ok, so with Automator we can add an AppleScript document and then build an app. This should be fast, and BTW you can easily add your own icon for the newly created app. And then just send it to Mac App Store ;). But before that, I suggest reading some tutorials about cleaning temporary files from XCode. If you have important data, you should be careful with deleting files from some directories (CoreSimulator for example). Personally, I recommend a blog post by Ajith R Nayak It describes why deleting some files from XCode is not always the best solution.

The whole code of our XCode Cleaner app you can download from our repository. Please comment, if you have any questions or improvements. In the next tutorial I'll try to show how to make a Xcode plugin with the same abilities. Godspeed!

Photo by Patrick Lindenberg from Unsplash

Check our latest product - it's based on our experience of managing over 50-people strong company. The tool we're missing as a small company and not an enterprise.

humadroid.io is an employee and performance management software. It's an unique tool allowing everyone to be in the loop - by having up to date info about co-workers, time-off, benefits, assets, helping with one-on-ones, being a go-to place for company-wide announcements.

Check out humadroid.io
Top

Contact us

* Required fields

The controller of your personal data provided via this contact form is Prograils sp. z o.o., with a registered seat at Sczanieckiej 9A/10, 60-215 Poznań. Your personal data will be processed in order to respond to your inquiries and for our marketing purposes (e.g. when you ask us for our post-development, maintenance or ad hoc engagements for your app). You have the rights to: access your personal data, rectify or erase your personal data, restrict the processing of your personal data, data portability and to object to the processing of your personal data. Learn more.

Notice

We do not track you online. We use only session cookies and anonymous identifiers for the purposes specified in the cookie policy. No third-party trackers.

I understand
Elo Mordo!Elo Mordo!