Applescript Error -4960 after MAC OS Update - applescript

I keep getting an error when I try to run this as part of a much bigger script. Doesn't like "to the folders". Only thing that changed on my end was an update to the MAC OS operating system (currently on Big Sur 11.6.5).
tell application "Finder" to set myFolders to the folders of theDestinationFolder
set folderCount to count myFolders
set loggingCounter to 0
repeat with aFolder in myFolders
set progress description to "Processing folder " & (loggingCounter + 1) & " of " & folderCount
set progress additional description to ""
set progress total steps to folderCount
set progress completed steps to loggingCounter
organizeSubfolder(aFolder)
set loggingCounter to loggingCounter + 1
end repeat

Related

Progress Bar in Applescript window

I want to download different apps with a bash script on macOS.
As there are some larger downloads (e.g. Office 365) I would like to include a progress bar in a regular macOS Window.
The applications download+install script looks like this
cd ~/Downloads/ && { curl -O https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg ; cd -; }
sudo hdiutil attach ~/Downloads/googlechrome.dmg
sudo cp -r /Volumes/Google\ Chrome/Google\ Chrome.app /Applications/
diskutil unmount Google\ Chrome
But this does not show any progress for the user (script will run in background).
I found the following example and edited it a bit to my likings
chromedl() {
osascript <<AppleScript
set progress description to "Loading"
set progress additional description to "Please wait"
set progress total steps to 3
repeat with i from 1 to 4
set theSourceCode to do shell script "curl -L -m 30 seconds [url=https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg]https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg]"
set progress completed steps to i
end repeat
display dialog "Progress bar is complete."
AppleScript
But this creates a curl execution error.
Please help me with a functioning Curl Download + Progress Bar script
A basic progress spinner window is easy enough to create using AppleScriptObjectiveC. Copy the following into Script Editor and run it in the foreground (command-control-R, or use the menu command Script→Run in Foreground, which appears when you hold down the control key).
use framework "AppKit"
use AppleScript version "2.4" -- Yosemite 10.10 or later required
use scripting additions
-- convenience properties to make ASOC more readable
property ca : current application
property NSPanel : class "NSPanel"
property NSView : class "NSView"
property NSProgressIndicator : class "NSProgressIndicator"
property NSTextField : class "NSTextField"
property HUD_mask : 8192
property nonactivating_mask : 128
global progress_panel, progress_indicator, label_field
make_progress_panel()
progress_indicator's startAnimation:me
progress_panel's orderFront:me
-- just hang it out there for 5 seconds as proof of concept
delay 5
progress_panel's |close|()
on make_progress_panel()
-- make floating panel
set content_rect to ca's NSMakeRect(200, 800, 140, 80)
set progress_panel to NSPanel's alloc's initWithContentRect:content_rect styleMask:(HUD_mask + nonactivating_mask) backing:(ca's NSBackingStoreBuffered) defer:true
set progress_panel's floatingPanel to true
-- make progress indicator
set progress_rect to ca's NSMakeRect(0, 0, 40, 40)
set progress_indicator to NSProgressIndicator's alloc's initWithFrame:progress_rect
set progress_indicator's indeterminate to true
set progress_indicator's |style| to 1
set progress_indicator's translatesAutoresizingMaskIntoConstraints to false
--make text field
set label_field to NSTextField's labelWithString:"Downloading"
set label_field's translatesAutoresizingMaskIntoConstraints to false
-- make container view, and add subviews
set content_view to NSView's alloc's initWithFrame:content_rect
content_view's addSubview:label_field
content_view's addSubview:progress_indicator
progress_indicator's sizeToFit()
set progress_panel's contentView to content_view
-- view constraints, for alignment
set pi_y_centering to progress_indicator's centerYAnchor's constraintEqualToAnchor:(content_view's centerYAnchor)
set lf_y_centering to label_field's centerYAnchor's constraintEqualToAnchor:(content_view's centerYAnchor)
pi_y_centering's setActive:true
lf_y_centering's setActive:true
set lf_left_margin to content_view's leadingAnchor's constraintEqualToAnchor:(label_field's leadingAnchor) |constant|:-10
lf_left_margin's setActive:true
set pi_left_margin to label_field's trailingAnchor's constraintEqualToAnchor:(progress_indicator's leadingAnchor) |constant|:-10
pi_left_margin's setActive:true
set pi_right_margin to progress_indicator's trailingAnchor's constraintGreaterThanOrEqualToAnchor:(content_view's trailingAnchor) |constant|:10
pi_left_margin's setActive:true
end make_progress_panel
The simplest way to make this work with curl, I think, is to detach the curl process entirely and recover its pid, like so (the code at the end detaches the process and sends the output to oblivion, then returns the pid):
set curl_pid to do shell script "curl -L [url=...] &> /dev/null & echo $!"
Once you have the pid you can set up a loop (or an idle handler, if you create an app) and periodically check to see if the process is still active:
try
set b to do shell script "pgrep curl | grep " & curl_pid
on error
progress_indicator's stopAnimation:me
progress_panel's |close|()
end try
If you want a determinate progress bar, that's easy enough to manage, but you have to figure out a way to measure progress. This link suggests you can get the headers for a file first and extract the file size; you could compare that against the actual on-disk file size as the download progresses, and feed the ratio into the progress bar.

Play custom bundled sound in apple script

So im trying to use shellscript "afplay " in applescript to play an .m4a file I bundled into the app resource. Not entirely sure how it works.
Got this after a bit of searching around,
set soundPath to POSIX path of (path to resource "Sound File.aiff")
do shell script "afplay " & quoted form of soundPath
This isnt really what I want.
playAudio() -- place this Line To call the handler to Play An Audio File.
on playAudio()
set audioFile to path to resource "Sound File.aiff"
set theFile to POSIX path of audioFile
do shell script "afplay -q 1 -t 25 " & " " & quoted form of theFile
end playAudio
-- Command Line OPTIONS For Playing Audio Files
(*
afplay [option...] audio_file
Options: (may appear before or after arguments)
{-v | --volume} VOLUME
set the volume for playback of the file
{-h | --help}
print help
{ --leaks}
run leaks analysis
{-t | --time} TIME
play for TIME seconds
{-r | --rate} RATE
play at playback rate
{-q | --rQuality} QUALITY
set the quality used for rate-scaled playback (default is 0 - low quality, 1 - high quality)
{-d | --debug}
debug print output
*)

VBS script to scan and install all devices

I'm trying to figure out a way to scan all devices (without drivers installed) and install them one by one automatically.
I've made a simple script that adds/removes a registry value for driver locations, since we have a server with all the current drivers and it's updated frequently, so instead of pointing device manager to that location manually the script does it for me.
Problem is we work in a production environment and we have a lot of different devices to install, and doing it manually takes too long, even with the script i have to click each device and update the driver, the scripts just makes it a little easier by pointing it to the server with the drivers.
So basically i'm try to make the script add the location (this works fine ATM) and them update each device without prompting the user.
Option Explicit
Set ws = WScript.CreateObject("WScript.Shell")
Dim s, ws, rl
rl = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\"
s = InputBox("Please select what you want to do" & _
vbCrLf & vbTab & "1 - Clear all, set default driver path." & _
vbCrLf & vbTab & "2 - Default path + production drivers" & _
vbCrLf & vbTab & "3 - Default and production path + Skylake drivers")
If s = 1 then
ws.RegWrite rl & "DevicePath", "%SystemRoot%\inf" , "REG_EXPAND_SZ"
ElseIf s = 2 then
ws.RegWrite rl & "DevicePath", "%SystemRoot%\inf; B:\LocalDrivers\; \\ccdsrv01\shares\Resources\Drivers\Client" , "REG_EXPAND_SZ"
ElseIf s = 3 then
ws.RegWrite rl & "DevicePath", "%SystemRoot%\inf; B:\LocalDrivers\; \\ccdsrv01\shares\Resources\Drivers\Client; \\ccdsrv01\shares\Resources\PreProd\SkyBay (Skylake-SunrisePoint)\New" , "REG_EXPAND_SZ"
End If

Getting errors when checking framentation status

I am trying to execute the following script on Win7 (x64) to check if any volumes need to be defragmented.
Set VolumeList = GetObject("winmgmts:").ExecQuery("Select * from Win32_Volume")
For Each objVolume in VolumeList
errResult = objVolume.DefragAnalysis(blnRecommended, objReport)
If errResult = 0 then
Wscript.Echo "Used space: " & objReport.UsedSpace
Wscript.Echo "Volume name: " & objReport.VolumeName
Wscript.Echo "Volume size: " & objReport.VolumeSize
If blnRecommended = True Then
Wscript.Echo "This volume should be defragged."
Else
Wscript.Echo "This volume does not need to be defragged."
End If
Wscript.Echo
Else
MsgBox errResult
End If
Next
I have tried to run this script on two different Win7 systems.
On the first, I get an OUT OF MEMORY error on GetObject("winmgmts:").ExecQuery("Select * from Win32_Volume").
On the second, I get no OUT OF MEMORY error on GetObject, but I get error 11 (Unknown Error) in errResult (output of DefragAnalysis-method).
Both Win7 systems have been installed and configured in the same way.
Perhaps this is not important, but when I check the WMI properties, it says "Connected to <Local Computer>" and not (as in Win XP) "SUCCESSFULLY connected to <Local Computer>".
Code works just fine for me, but perhaps it'll help when you explicitly connect to the right namespace:
Set wmi = GetObject("winmgmts://./root/cimv2")
Set VolumeList = wmi.ExecQuery("SELECT * FROM Win32_Volume")
Also I'd recommend restricting the query to just local disks that have a drive letter assigned to them:
SELECT * FROM Win32_Volume WHERE DriveType = 3 AND DriveLetter IS NOT NULL
Use WBEMTest or WMIDiag to check if your WMI connection is working at all. Check the Application and System eventlogs for errors and warnings, too.
The reason for the error 11 was that the script was not run with elevated privileges. Once it was run as administrator, it worked fine. Thanks

Running a vbs file via a scheduled task on Server 2003

I've been working on modifying an existing vbscript. The wierd part is that when I run the script manually, it works fine. But as soon as I try to run it as a scheduled task, it reports as complete, but doesn't actually do anything. After much troubleshooting, I think I tracked it down to the original CreateObject. Here's the code:
On Error Resume Next
'create an instance of IE
Dim oIE, objFSO, linenum
linenum = 0
Set oIE = CreateObject("InternetExplorer.Application")
'If err.number <> 0 Then linenum = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile ("C:\test.txt", ForAppending, True)
'objTextFile.WriteLine(now() & " Internet object created.")
'Execute our URL
'oIE.navigate("<intranet site>")
'objTextFile.WriteLine(now() & " Starting import")
'wait for the window to be closed (exit IE)
'Do Until Err : oIE.visible = True : wsh.sleep 1000 : Loop
'objTextFile.WriteLine(now() & " Import complete.")
if Err.Number <> 0 then
' An exception occurred
objTextFile.WriteLine("Exception:" & vbCrLf & " linenum: " & linenum & vbCrLf & " Error number: " & Err.Number & vbCrLf & " Error source: " & Err.source & vbCrLf & " Error description: " & Err.Description & vbCrLf)
End If
'clean up
'oIE.Quit
'oIE.Visible = False
'Set oIE = Nothing
I've commented most of it out, to narrow it down, and from the logging I added, it spits out the current error:
Exception:
linenum: 0
Error number: -2147467259
Error source:
Error description:
Yes, the source and description lines are blank.
Googling the error doesn't seem to bring up anything useful. So I'm not sure what's going on. Permissions have been checked multiple times, and it's always run as Administrator, the same user as I'm logged in as. The funny part is, this script works fine with Windows 2000. About the only thing I can think of is perhaps the Remote Desktop connection I'm using is somehow interfering with it.
Anyone have any ideas or things I might be able to try to resolve this?
For reference, when you've got problems googling a decimal error number, try converting it to hexadecimal. -2147467259 is the same as 80004005 and if you search for that you'll find that it's quite a common error and usually means that you're denied access to something so even if you're sure that it's not permissions for the things you've checked, it might be worth doing the following checks:
Does the scheduled task run under the same account as you used when you executed the script manually? Otherwise, try doing a RunAs on the script to run as the same user account as the task, if that works, try scheduling the task as your account.
That way you'll know if it's (task vs manual) or if it's (user1 vs user2). Which might make it a little easier to track down the issue.

Resources