Sunday, December 4, 2022

#1 2022-11-10 01:11:22 pm

One208
Member
Registered: 2021-03-15
Posts: 65

get Path to disk

Hi
trying to write a script to automate the creation of MacOS installer.
Plz suggest how I can
A. "do shell script" opens a terminal window to display the progress of the process
B. Get path to removable disk whose name is "No Name" (Selected from disc names)
Thank you
Below is script which works but no progress indicator

Applescript:


tell application "System Events" to set diskList to (get the name of every disk where it is ejectable)
set theDisk to choose from list diskList with prompt "Select which Disc to Install:"
set diskPath to "/Volumes/" & theDisk
-- Select MacOS Installer
set theMacOS to POSIX path of (choose file with prompt "Please MacOS Installer:" default location (path to "apps")) as text
-- User name & password
set userName to short user name of (system info)
display dialog "Installer will need an Admin User name and password in order to make your changes." & return & return & "Please enter an admin password." default answer "" with icon 2 with hidden answer
set pword to text returned of result
-- Command
set thePath to quoted form of theMacOS & ("Contents/Resources/createinstallmedia --volume ") & quoted form of diskPath & (" --nointeraction")
-- Run the Script
do shell script thePath user name userName password pword with administrator privileges

Last edited by One208 (2022-11-10 01:14:12 pm)

Offline

 

#2 2022-11-10 05:58:49 pm

One208
Member
Registered: 2021-03-15
Posts: 65

Re: get Path to disk

This script runs the command in Terminal

Applescript:


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
-- Select Removable Disk to Instal upon
tell application "System Events" to set diskList to (get the name of every disk where it is ejectable)
set theDisk to choose from list diskList with prompt "Select which Disc to Install:"
set diskPath to "/Volumes/" & theDisk
-- Select MacOS Installer
set theMacOS to POSIX path of (choose file with prompt "Please MacOS Installer:" default location (path to "apps")) as text

-- Run in Terminal window
inTerminal(diskPath, theMacOS)
-- Run in Script Edito (No progress Bar)
--inEditor(diskPath, theMacOS)

on inTerminal(diskPath, theMacOS)
   -- Escape white space of paths
   set theMacOS to findAndReplaceInText(theMacOS, " ", "\\ ") of me
   set diskPath to findAndReplaceInText(diskPath, " ", "\\ ") of me -- escape white space
   -- Create Command
   set thePath to "sudo " & theMacOS & "Contents/Resources/createinstallmedia --volume " & diskPath & " --nointeraction"
   -- Run command in Terminal
   doComInTerminalWindow(thePath) of me
end inTerminal

on inEditor(diskPath, theMacOS)
   -- Gather Info to run command with Admn privileges aka Sudo
   -- User Name
   set userName to short user name of (system info)
- For Password
   display dialog "Installer will need an Admin User name and password in order to make your changes." & return & return & "Please enter an admin password." default answer "" with icon 2 with hidden answer
   set pword to text returned of result
   -- Create Command
   set thePath to quoted form of theMacOS & ("Contents/Resources/createinstallmedia --volume ") & quoted form of diskPath & (" --nointeraction")
   -- Run the Script
   do shell script thePath user name userName password pword with administrator privileges
end inEditor
--================================================================
--» HANDLER
--================================================================
on doComInTerminalWindow(aCMD as string)
   using terms from application "Terminal"
       tell application id "com.apple.Terminal"
           activate
           set wCount to count (every window whose visible is true)
           if wCount = 0 then
               -- If no terminal window visible
               do script aCMD
           else
               -- Terminal window Visible
               do script aCMD in front window
           end if
       end tell
   end using terms from
end doComInTerminalWindow

on findAndReplaceInText(theText, theSearchString, theReplacementString)
   set AppleScript's text item delimiters to theSearchString
   set theTextItems to every text item of theText
   set AppleScript's text item delimiters to theReplacementString
   set theText to theTextItems as string
   set AppleScript's text item delimiters to ""
   return theText
end findAndReplaceInText
--» End of Handler ===================================================

Offline

 

#3 2022-11-11 09:00:49 am

robertfern
Member
Registered: 2011-11-29
Posts: 225

Re: get Path to disk

I modified your script to have the output from the 'createinstallmedia' command go to a temp text file and also run in the background. So the script can monitor the text file for progress.

I have one glaring problem.

When you use 'createinstallmedia' in a terminal without '--nointeraction' it pauses waiting for user input, but it output the correct text  below:

Copying to disk:  0%... 10%... 20%... 30%... 40%... 50%... 60%... 70%... 80%... 90%... 100%

With '--nointeraction' it doesn't pause for user input (which we want), but it only outputs this:

Copying to disk:  0%... 100%


Here is the script:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "System Events" to set diskList to (get the name of every disk whose ejectable is true)
set theDisk to choose from list diskList with prompt "Select which Disc to Install:"
set theDisk to "Untitled" -- TESTING
set diskPath to "/Volumes/" & theDisk
-- Select MacOS Installer
set theMacOS to POSIX path of (choose file with prompt "Please Choose MacOS Installer:" default location (path to "apps")) as text
-- User name & password
set userName to short user name of (system info)
set pword to text returned of (display dialog "Installer will need an Admin User name and password in order to make your changes." & return & return & "Please enter an admin password." default answer "" with icon 2 with hidden answer)
-- Command
set thePath to quoted form of theMacOS & ("Contents/Resources/createinstallmedia --volume ") & quoted form of diskPath & (" --nointeraction >/Users/Shared/cim.txt 2>&1 &")
-- Run the Script
get thePath
set thePID to do shell script thePath user name userName password pword with administrator privileges
set progress description to "Creating bootable USB installer…"
set progress total steps to 10
set progress completed steps to 0
set pnum to 0
repeat
   set progString to get_Progress()
   if class of progString is not boolean then
       try
           set pnum to (last word of progString) as integer
           set progress completed steps to (pnum div 10)
           set progress additional description to first paragraph of progString
       end try
       if pnum = 100 then exit repeat
   end if
   delay 3
end repeat
-- delete progress log file
tell application "System Events" to delete file ((path to shared documents as text) & "cim.txt")

on get_Progress()
   local cfile, progString, flag
   try
       set cfile to open for access ((path to shared documents as text) & "cim.txt")
   on error
       return false
   end try
   try
       set progString to read cfile from 1 as text
   on error
       set progString to false
   end try
   close access cfile
   if class of progString is text then
       set progString to paragraphs of progString
       repeat with i in progString
           set i to contents of i
           if "Copying to disk" is in i then
               set progString to i
               exit repeat
           end if
       end repeat
       if class of progString is list then set progString to "0"
   end if
   return progString
end get_Progress

Last edited by robertfern (2022-11-11 01:04:25 pm)

Offline

 

#4 2022-11-15 02:18:40 pm

robertfern
Member
Registered: 2011-11-29
Posts: 225

Re: get Path to disk

I figured out how to get the progress using the unix 'lsof' command.

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

global userName, pword, thePID

on run
   local diskList, theDisk, diskPath, theMacOS, theCMD, PCT, fSize, dmg
   set {PCT, dmg} to {0, ""}
   tell application "System Events" to set diskList to (get the name of every disk whose ejectable is true)
   set diskPath to choose from list diskList with prompt "Select which Disc to Install:"
   if class of diskPath is boolean then return
   set diskPath to "/Volumes/" & diskPath
   -- Select MacOS Installer
   set installPath to (choose file with prompt "Please Choose MacOS Installer:" default location (path to "apps")) as text
   if class of installPath is boolean then return
   set installPath to POSIX path of installPath
   set dmg to installPath & "Contents/SharedSupport/SharedSupport.dmg"
   -- User name & password
   set userName to short user name of (system info)
   set pword to text returned of (display dialog "Installer will need an Admin User name and password in order to make your changes." & return & return & "Please enter an admin password." default answer "" with icon 2 with hidden answer)
   -- Command
   set theCMD to quoted form of installPath & ("Contents/Resources/createinstallmedia --volume ") & quoted form of diskPath & (" --nointeraction >/dev/null 2>&1 & echo $!")
   set fSize to size of (info for POSIX file dmg)
   -- Run the Script
   set thePID to do shell script theCMD user name userName password pword with administrator privileges
   set progress description to "Creating bootable USB installer…"
   set progress total steps to 100
   set progress completed steps to 0
   set tid to text item delimiters
   repeat
       --do shell script ("ps -ax -o pid= " & thePID)
       set writeNum to get_Progress()
       if class of writeNum is boolean then
           if PCT = 99 then
               set PCT to 100
           else
               display alert "Error copying files!" giving up after 10
               return
           end if
       end if
       if writeNum = 0 then
           if PCT = 99 then set PCT to 100
       else
           set PCT to writeNum / fSize * 100 div 1
       end if
       set progress completed steps to PCT
       set progress additional description to "copying is " & PCT & "% done."
       delay 5
       if PCT = 100 then exit repeat
   end repeat
   set text item delimiters to tid
   display alert "Installer creation done!" giving up after 10
end run

on get_Progress()
   local lsof, flag, fLen
   set text item delimiters to "/"
   try
       set lsof to paragraphs of (do shell script ("sudo lsof -p " & thePID) user name userName password pword with administrator privileges)
   on error
       return false
   end try
   set fLen to 0
   repeat with i in lsof
       set mpath to rest of (text items of i)
       if (count mpath) > 0 then
           if (item 1 of mpath) = "Volumes" then
               if (last item of mpath) = "SharedSupport.dmg" then
                   set fLen to (word 7 of i) as integer
                   exit repeat
               end if
           end if
       end if
   end repeat
   return fLen
end get_Progress

if you make this script a standalone app, the progress bar will be a floating window

**EDIT** - cleaned up some error checking in script

Last edited by robertfern (2022-11-15 08:32:52 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)