Jump to content

Workflow gives error when running AppleScript


Recommended Posts

When running this workflow I see the following error in Alfred's debug pane. No output makes it to the notification action.

 

 

[ERROR: alfred.workflow.action.applescript] {

    NSAppleScriptErrorAppName = "Alfred 2";

    NSAppleScriptErrorBriefMessage = "every item of trash doesn\U2019t understand the \U201ccount\U201d message.";

    NSAppleScriptErrorMessage = "every item of trash doesn\U2019t understand the \U201ccount\U201d message.";

    NSAppleScriptErrorNumber = "-1708";

    NSAppleScriptErrorRange = "NSRange: {425, 14}";

}

 

Hard coding a value for the relevant line causes the workflow to run fine. The script runs fine in AppleScript editor (although bizarrely it gives a value of zero for the number of items in my trash - so if anyone knows why that is I'd be grateful for their input).

Link to comment

When running this workflow I see the following error in Alfred's debug pane. No output makes it to the notification action.

 

 

Hard coding a value for the relevant line causes the workflow to run fine. The script runs fine in AppleScript editor (although bizarrely it gives a value of zero for the number of items in my trash - so if anyone knows why that is I'd be grateful for their input).

 

The code below seems to work (does not verify if trash is empty):

set trash_size to (do shell script "du -sk ~/.Trash/ | awk '{print $1}'") as integer
set units to {"KB", "MB", "GB", "TB"}
set i to 1

repeat while trash_size is greater than 1000
	set trash_size to trash_size / 1000
	set i to i + 1
end repeat

set trash_size to roundTo(trash_size, 1)

tell application "Finder" to set num_items to count (every item in trash)

trash_size & item i of units & " for " & (num_items) & " items" as string

on roundTo(n, numDecimals)
	set x to 10 ^ numDecimals
	(((n * x) + 0.5) div 1) / x
end roundTo

Note that I set i to 1 initially and call Finder to get trash items.

Edited by Carlos-Sz
Link to comment

Awesome! Thank you! Any idea what I was doing wrong? Both for the error message and the 0 count for items in the trash.

Having a very small file in trash lead the 'i' equal to zero which is not a valid index in your units list.

In addition you have to ask Finder about trash items which the original code didn't.

Link to comment

But why did the same code run fine in AppleScript Editor and not in Alfred?

That's interesting. Is that a recent(ish) change? I found numerous bits of code online for getting the number of items in the trash via AppleScript and none of them asked the Finder.

Link to comment

The original code didn't work in my AppleScript Editor (Script Debugger).

But why did the same code run fine in AppleScript Editor and not in Alfred?

That's interesting. Is that a recent(ish) change? I found numerous bits of code online for getting the number of items in the trash via AppleScript and none of them asked the Finder.

Link to comment

Huh. It worked just fine in mine. No errors. Weird.

 

If I run the original code in AppleScript Editor the "(count of items) in trash” returns zero items but if I type “tell application Finder” then it will return the number of items in trash properly.

 

Anyway, I don’t know why AppleScript Editor is not showing an error.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...