Jump to content
liatmgat

Help Me Make a Workflow | Display File Size in Large Type

Recommended Posts

Hi amazing clever Alfred-ites,

 

I would like to make a workflow that would display the size of a selected file in large type. So if I have a file selected in the Finder, I could hit a keystroke and, really big, I would see "1.2 GB" or whatever the file size is.

 

I don't even know where to start. Does anyone have any ideas?

 

Thank you!

 

Liat

Share this post


Link to post

First, create a new workflow (BLANK)

 

From top right hit the + button and select "Triggers" - "Hotkey"

Double click the hotkey trigger, and set your hotkey

Then for Action, select "Pass through to workflow"

Then for Argument, select "Selection in OS X"

Click Save

 

Again click the + at the top right and select "Actions" - "Run Script"

Double click on the Run Script and for language select "osascript (AS)"

Deselect all the Escaping options.

In the script area paste the following:

tell application "Finder"
set selectedItem to (item 1 of (get selection))
set informationList to {}
copy ("Size: " & size of selectedItem & " (" & physical size of selectedItem & ")") to end of informationList
end tell

Then Save

 

Then again, click the top right + and select "Outputs" - "Large Type"

 

Finally connect the Hotkey to the Run Script, and then the Run Script to the Large Type.

 

NOTE: There are many ways of getting file info, the above shows the exact file size and size on disc as shown when simply using CMD + I on a file.

Share this post


Link to post

Thank you so much - you are amazing! It's so cool that you know how to do this! I really appreciate it.

 

I set up the workflow like you said and it works.

 

However, the output is a little confusing if you're not a computer. It displays it like this:

 

Size: 5739242 (5783552)

 

Is there any way to get it to display like it does in the File Info window, like this? ->  Size: 5.7 GB

 

Again, thanks a million. 

 

Share this post


Link to post

Then for Argument, select "Selection in OS X"

 

You don't need this bit. You get the Finder selection in your AppleScript. 

 

Is there any way to get it to display like it does in the File Info window, like this? ->  Size: 5.7 GB

 

This should do the trick: 

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

on humanSize(bytes)
	set i to 1
	set n to bytes
	set units to {"B", "KB", "MB", "GB", "TB"}
	repeat while i < (count of units) and n > 1000
		if n > 1000 then
			set n to n / 1000
			set i to i + 1
		end if
	end repeat
	if i > 2 then
		set n to roundThis(n, 2)
	else
		set n to roundThis(n, 1)
	end if
	return (n as string) & " " & (item i of units) as string
end humanSize

tell application "Finder"
	set selectedItem to (item 1 of (get selection))
	set theBytes to (size of selectedItem)
	"Size: " & my humanSize(theBytes)
end tell

Don't ask me how the roundThis() function works.

Edited by deanishe

Share this post


Link to post

Thanks for chiming in deanishe!

I searched Google for the rounding and found so many it flustered me ... as I'm a bit more comfortable with BASH than OSA.

Share this post


Link to post

 

You don't need this bit. You get the Finder selection in your AppleScript. 

 

 

This should do the trick: 

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

on humanSize(bytes)
	set i to 1
	set n to bytes
	set units to {"B", "KB", "MB", "GB", "TB"}
	repeat while i < (count of units) and n > 1000
		if n > 1000 then
			set n to n / 1000
			set i to i + 1
		end if
	end repeat
	if i > 2 then
		set n to roundThis(n, 2)
	else
		set n to roundThis(n, 1)
	end if
	return (n as string) & " " & (item i of units) as string
end humanSize

tell application "Finder"
	set selectedItem to (item 1 of (get selection))
	set theBytes to (size of selectedItem)
	"Size: " & my humanSize(theBytes)
end tell

Don't ask me how the roundThis() function works.

 

 

OH MY GOD YOU ARE AMAZING!!! You have made my life so much easier! Thank you, thank you from the bottom of my heart.

Share this post


Link to post

OH MY GOD YOU ARE AMAZING!!! You have made my life so much easier! Thank you, thank you from the bottom of my heart.

 

If that's directed at me, I just pasted a few lines of code I found into Rodger's script. Thank him.

 

Left as an exercise to the reader is changing the bytes (and possibly KB) to have zero decimal places and to remove trailing zeroes (and decimal points) from the output.

 

Thanks for chiming in deanishe!

I searched Google for the rounding and found so many it flustered me ... as I'm a bit more comfortable with BASH than OSA.

 

I hate both of them, to be honest. Give me a "real" programming language like Python or Ruby any day. Things you come to take for granted in a programming language are usually super-esoteric in bash and overly-complicated in AppleScript.

 

I think I got the rounding code from Stack Overflow. Like I say, I don't really understand exactly how it works, but I tested it, and it does!

 

EDIT: Tell a lie, the code is from here.

 

EDIT 2: I've had a look at the AppleScript docs and now I do understand how roundThis() works. I'd never have thought of it myself, though…

Edited by deanishe

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...