--- title: "Install DMG Files using Shell Script" slug: "install-dmg-files-using-shell-script" description: "Discover how to install .dmg files on macOS devices using shell scripts with Scalefusion. This guide provides a step-by-step walkthrough for script creation and .dmg file deployment." updated: 2024-09-05T09:45:31Z published: 2024-09-05T09:45:31Z canonical: "help.scalefusion.com/install-dmg-files-using-shell-script" --- > ## Documentation Index > Fetch the complete documentation index at: https://help.scalefusion.com/llms.txt > Use this file to discover all available pages before exploring further. # Install DMG Files using Shell Script Scalefusion offers support to distribute Apple VPP apps and PKG (product archive packages) natively using the Dashboard. Most Enterprise apps use either of the above mechanisms for distribution in an enterprise scenario. However, there might be certain cases where the developer has made the installer available in the form of a **DMG** (Disk Image File). In such cases where only a **DMG file is** available, IT Admins can use the script described below and deploy it via Scalefusion MDM agent to remotely install the DMG. Please follow the steps below to use this method. Note: 1. This method uses a script to install the DMG file; though Scalefusion has tested these scripts, please validate the scripts on a test machine before deploying them on all your managed devices. 2. Scalefusion will not be responsible for any loss of data or system malfunction that may arise due to the usage of these scripts ## Prerequisites 1. Deploy [Scalefusion MDM Agent](/docs/scalefusion-mdm-agent-application-for-mac-devices). 2. Scalefusion subscription plan with access to Content ManagementDMG files that wrap a PKG file that, when extracted, contains a PKG file cannot be distributed by this mechanism. We suggest to use the PKG file directly in such cases. ## Step 1: Generate a Download URL for the DMG file The first step is to generate a download URL for the DMG file that needs to be installed. You can use any of your servers (if you have one already) or a storage provider like Amazon S3 or Cloudflare R2 to upload your DMG and generate a public download URL. In case you don't have a storage provider that you use, you can also use **Scalefusion's Content Management** to upload your DMG files and obtain a DMG URL. Follow the steps below to upload the file to Scalefusion's content management and generate the download URL, 1. Navigate to **Content Management > Content** and click **Upload** File to upload the DMG.![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image%20-%202023-09-08T154634.287.png) 2. Wait for the DMG file to be uploaded and to appear in the list of files. Once the file appears, click on the file to view the file details window.![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image%20-%202023-09-08T154655.729.png) 3. Click on the 3 dots, and from the menu, select **Copy Download Link![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image%20-%202023-09-08T154718.215.png)** 4. Now you can open TextEdit (or any text editor) on your Mac/PC and paste the copied URL to your local machine. The URL would look like below,URLURL ```custom https://mobilock.s3.amazonaws.com/uploads/mobile_file/content/5947/rustdesk-1.1.9.dmg ``` ## Step 2: Create the Script 1. Create a file on your Mac, for example, **download_install_dmg.sh** and open it to a text editor of your choice like **TextEdit** 2. Copy the contents below.ShellShell ```shell #!/bin/bash # Insert the DMG download url below DownloadUrl="DMG_download_url" ##### DONOT EDIT BELOW CODE ##### regex='^https.*.dmg$' if [[ $DownloadUrl =~ $regex ]]; then echo "URL points to direct DMG download" validLink="True" else echo "Searching headers for download links" urlHead=$(curl -s --head "$DownloadUrl") locationSearch=$(echo "$urlHead" | grep https:) if [ -n "$locationSearch" ]; then locationRaw=$(echo "$locationSearch" | awk '{print $2}') locationFormatted="$(echo "${locationRaw}" | tr -d '[:space:]')" regex='^https.*' if [[ $locationFormatted =~ $regex ]]; then echo "Download link found" DownloadUrl="$locationFormatted" else echo "No https location download link found in headers" exit 1 fi else echo "No location download link found in headers" exit 1 fi fi # Create Temp Folder DATE=$(date '+%Y-%m-%d-%H-%M-%S') TempFolder="Download-$DATE" mkdir -p "/tmp/$TempFolder" # Navigate to Temp Folder cd "/tmp/$TempFolder" || exit # Download File into Temp Folder curl -s -O "$DownloadUrl" # Capture name of Download File DownloadFile="$(ls)" echo "Downloaded $DownloadFile to /tmp/$TempFolder" # Verify DMG File regex='\.dmg$' if [[ $DownloadFile =~ $regex ]]; then DMGFile="$DownloadFile" echo "DMG File Found: $DMGFile" else echo "File: $DownloadFile is not a DMG" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 fi # Mount DMG File (-nobrowse prevents the volume from popping up in Finder) hdiutilAttach=$(hdiutil attach "/tmp/$TempFolder/$DMGFile" -nobrowse) echo "Used hdiutil to mount $DMGFile" err=$? if [ ${err} -ne 0 ]; then echo "Could not mount $DMGFile Error: ${err}" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 fi regex='\/Volumes\/.*' if [[ $hdiutilAttach =~ $regex ]]; then DMGVolume="${BASH_REMATCH[0]}" echo "Located DMG Volume: $DMGVolume" else echo "DMG Volume not found" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 fi # Identify the mount point for the DMG file DMGMountPoint=$(hdiutil info | grep "$DMGVolume" | awk '{ print $1 }') echo "Located DMG Mount Point: $DMGMountPoint" # Capture name of App file cd "$DMGVolume" || exit AppName=$(ls | grep '.app') cd ~ || exit echo "Located App: $AppName" # Test to ensure App is not already installed ExistingSearch=$(find "/Applications/" -name "$AppName" -depth 1) if [ -n "$ExistingSearch" ]; then echo "$AppName already present in /Applications folder" hdiutil detach "$DMGMountPoint" echo "Used hdiutil to detach $DMGFile from $DMGMountPoint" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 else echo "$AppName not present in /Applications folder" fi DMGAppPath=$(find "$DMGVolume" -name "*.app" -depth 1) # Copy the contents of the DMG file to /Applications/ # Preserves all file attributes and ACLs cp -pPR "$DMGAppPath" /Applications/ err=$? if [ ${err} -ne 0 ]; then echo "Could not copy $DMGAppPath Error: ${err}" hdiutil detach "$DMGMountPoint" echo "Used hdiutil to detach $DMGFile from $DMGMountPoint" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 fi echo "Copied $DMGAppPath to /Applications" # Unmount the DMG file hdiutil detach "$DMGMountPoint" echo "Used hdiutil to detach $DMGFile from $DMGMountPoint" err=$? if [ ${err} -ne 0 ]; then echo "Could not detach DMG: $DMGMountPoint Error: ${err}" fi # Remove Temp Folder and download rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" ``` 3. Now replace the phrase "DMG_download_url" with the download URL generated in Step 1.ShellShell ```shell # Insert the DMG download url below DownloadUrl="DMG_download_url" ``` 4. Save the file on your Mac. ## Step 3: Deploy the Script Please use our help document on [deploying scripts on macOS devices](/docs/execute-shell-scripts-on-macos-devices-with-scalefusion) to upload and publish the script to your managed Mac devices. For easy reference, here are the steps, 1. Navigate to **Application Management > Enterprise Store**& upload the script as shown below,![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image%20-%202023-09-08T155036.622.png) 1. E**nter Script Name:** Enter a name for the script file 2. **Upload Script File:** Upload the script file created in Step 2 3. **Run Script as signed-in user:** Select **Yes** from the dropdown 4. Click **Save** 2. Now you can **Publish** the script to the device profiles where you want to install this application.![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image%20-%202023-09-08T155128.975.png) 3. Once the script has been deployed, you can **View Status**- from the side panel that appears once you click on the uploaded script file. Here are sample screenshots, 1. **Successful Installation**![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image%20-%202023-09-08T155210.173.png) 2. **Invalid Download URL**![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image%20-%202023-09-08T155212.194.png) 3. **Failed to Install DMG File**![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image%20-%202023-09-08T155215.150.png) If you would like to install a DMG from a local file on the managed device, Download the script [here](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/install%20a%20DMG%20from%20a%20local%20file.txt) and replace the ENTER_PATH_NAME phrase with the path of the local file on the managed Mac device.