Jump to content

Converting an Applescript service to an Alfred workflow.


Recommended Posts

Hi everyone, 

 

I'm trying to convert an applescript I had set up as a service to an Alfred workflow, just because I like the idea of having all these custom tweaks in one place  :)

 

This is the applescript: http://bit.ly/1iu2nal 

 

This is the workflow: http://bit.ly/1gliP5H

 

I set the hotkey to be ⇧⌘>  (this is the same keyboard shortcut that I had set up to call the service).

 

I'm not sure if it's not working because you can't just copy and paste an applescript into an Alfred workflow like this or because my hotkey isn't working or both. I did a clean install of Maverick's over the weekend and haven't changed any of my default keyboard shortcuts, if that helps.

 

I'd really appreciate some help, 

 

Katie

 

Edit: Updated download links

Edited by katie
Link to comment

It looks like you just want to resize them all to a size defined in the code, right? You don't use the "input" and "parameters" variables that you're telling the script to use. I'm also gathering that because the keyword takes no arguments.

 

So, if you delete the "on run" statement, the "end run" statement, and the "return input" statement. Then the workflow works.

 

If you want to use the "on run" statements with Alfred, it seems to be better to use the "Run NSApplescript" than the osascript.

Link to comment

Thank you! It does work now and that's what I intended to use it for. Not really sure why though - if you don't mind me asking, does this mean that the code was wrong when running as a service or does it mean that you always have to remove those kind of lines when putting an applescript into Alfred (using the osascript option)?

 

I don't know any programming languages but I do like finding scripts online and customizing them...and I don't want to resort to asking for help all the time so maybe I can learn something from this  :)

Link to comment

Well, most of the code was commented out, so what you were using was only this:

set defaultWidth to 870
set defaultHeight to 440
set defaultSidebarWidth to 170

tell application "Finder"
	repeat with x from 1 to count of Finder windows
		set frontWindow to Finder window x
		set {windowLeft, windowTop, windowRight, windowBottom} to get bounds of frontWindow
		set bounds of frontWindow to {windowLeft, windowTop, windowLeft + defaultWidth, windowTop + defaultHeight}
		set sidebar width of frontWindow to defaultSidebarWidth
	end repeat
end tell

Basically, the "on" for Applescript does something like define a function that can be called at another point. I haven't written any services, but I'd assume that the services interface demands an "on run" statement with the "input" and "parameters" variables, and it demands that something be returned.

 

Theoretically, you could modify this service to resize any finder windows (or any windows, actually) to custom sizes that are specified at runtime. But, pretty much always, if a function says it needs a variable (and the on run {input, parameters} means that it needs the 'input' and 'parameters' variables), then you have to pass it those variables.

 

Alfred, however, doesn't pass those variables. And so the applescript freaked out because it didn't get what it wanted, and it also freaked out because it was trying to return the "input" that didn't exist.

 

You can pass variables to Applescript from Alfred, but those come in another form. Hopefully that helps explain it a bit.

Link to comment

Thank you!! That actually does help. A lot. I had tried looking it up but the result was a lot of jargon about handlers - you made it seem really simple.

 

Thank you for taking the time to solve the problem. And thank you for your efforts to teach me; I really appreciate it!

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...