Jump to content

Restart Alfred Workflow


Recommended Posts

Hey subject,

 

Thanks for making this, I have been meaning to do something like it for a while now. Anyways I did a little researching: MacRumors always has a forum!

and found that each key on a mac keyboard has a key code so to make alfred apear for you and me the applescript should look like this:

tell application "System Events" to key code 49 using command down
end

 

Simply search google for "key codes mac" to get an entire list. Lastly, make sure not to have "#" on the actual script only the comments. I understand you did that to make the keywords optional, but I could imagine that people in the future will try to enter a hotkey, but won't know to remove the "#".

 

Hope that helps!

Link to comment

No problem :-)

 

I'm a little confused though. Key code 49 still translates to an "I pressed the space bar" command, doesn't it? I was hoping to change this:

tell application "System Events"
	keystroke " " using command down # Edit with your own Alfred hotkey
end tell

 

To something like this:

tell application "System Events"
	keystroke (the Alfred hotkey for this user)
end tell

 

That way this workflow would work as-is, without requiring anyone to edit it with their own, personal Alfred hotkey (not everyone uses cmd+space, of course). Perhaps I've misunderstood what you were saying though?

 

As for using # to comment things out, I thought about the potential for confusion too, but I assumed that anyone brave enough to edit a shell script would know enough to figure that one out :-) I'm most comfortable with Python (with a dash of C and Java thrown in for good measure), and I'm used to just using the language's standard comment symbol to temporarily disable code. Are things different with shell scripts? Is there a more accepted way of disabling code, without actually removing it?

 

Just as an aside, I'm only allowed to make one more post for the next 18 hours, so my reply may be delayed ;-) It's a little frustrating, but I understand why the restriction's there.

Link to comment

curiosity: why would you need to restart Alfred? Do you need to do it so often that you need a workflow for it?

 

I think he wants to make a workflow for it so that when something isn't working you can restart Alfred to fix the bug (that has happened to me a few times). If that's not the reason, then he probably just wants to do it, because he can.

 

Side note: Do you need to have Alfred apear as confirmation? How about having a post notification? Although it might not work because Alfred restarts. I will keep looking around. Maybe the applescript could access where the hot key is stored in Alfred? We will soon find out.

Link to comment

curiosity: why would you need to restart Alfred? Do you need to do it so often that you need a workflow for it?

I actually made it because I was mucking around with themes and the blur hack, and got sick of having to restart Alfred after each change ;) Also...

 

He probably just wants to do it, because he can.

:D

 

Side note: Do you need to have Alfred apear as confirmation? How about having a post notification? Although it might not work because Alfred restarts. I will keep looking around. Maybe the applescript could access where the hot key is stored in Alfred? We will soon find out.

You're right, it wouldn't work. Alfred would send the Applescript off to be executed (that sounds really brutal :P), and the script would then terminate the Alfred 2 process before the workflow ever got around to posting a notification. You'd have to get the Applescript to post the notification itself, which would require the installation of third party tools. Personally, I actually think that having Alfred reappear is more elegant than posting a notification, but that's just me :)

 

As for finding a way to invoke Alfred without requiring that the hotkey be supplied manually, I have had some success! First I found the plist where the hotkey's stored (~/Library/Preferences/com.runningwithcrayons.Alfred-Preferences.plist), then I looked into ways of getting an Applescript to read a plist file, but then I was stuck. I could extract the key code for the pressed key easily enough, but I couldn't find a way of converting the key code for a modifier (eg: cmd = 1,048,576) into an actual key stroke. After going round in circles for a while I had to laugh when I found the solution on the Alfred forum itself:

tell application "Alfred 2" to search

I've updated the first post with a version that uses this code :)

Link to comment

curiosity: why would you need to restart Alfred? Do you need to do it so often that you need a workflow for it?

 

I have Alfred on my work computer and home computer and am constantly updating the workflows on one or the other. A restart is required to load the settings stored in iCloud by one computer to the settings on the other.

Edited by credford
Link to comment
  • 6 years later...

It's quite remarkable. I just had to updated the name "Alfred 2" to "Alfred 4" and everything still works as expected — even though this workflow is 6 years old at this point.

 

Great stuff and I love it. Just too many times I had to go through he interface and all.

 

I actually believe that Alfred should have a "Restart Alfred" by default.

Link to comment
  • 3 weeks later...
On 7/11/2019 at 8:35 AM, Chris Spiegl said:

It's quite remarkable. I just had to updated the name "Alfred 2" to "Alfred 4" and everything still works as expected — even though this workflow is 6 years old at this point.

 

Great stuff and I love it. Just too many times I had to go through he interface and all.

 

I actually believe that Alfred should have a "Restart Alfred" by default.

 

Agree, it's very useful. You may also update to 'tell application id "com.runningwithcrayons.Alfred"' (instead of 'tell application "Alfred 4"'), which should also work for future Alfred versions (Alfred 5, 6, ...). 

Link to comment

Hi @Subject22, I sent you a private message with the new version. Please test it and update your main download if everything works out normally. I don't want to provide a secondary download here because it can get way too confusing by doing so, having the current version be represented in the main forum post is best I think.

Link to comment
On 7/31/2019 at 9:04 PM, Chris Spiegl said:

Hi @Subject22, I sent you a private message with the new version. Please test it and update your main download if everything works out normally. I don't want to provide a secondary download here because it can get way too confusing by doing so, having the current version be represented in the main forum post is best I think.

 

Thanks! Unfortunately I took too long to get to it and the file transfer expired. Instead I’ve put the workflow on GitHub (and added a link to the first post). Please feel free to fork the repo and/or submit a pull request. I will endeavour to keep the first post in this thread in sync with the GitHub repo.

Link to comment

Hi @Subject22, that's awesome, instead of going through all the hustle of setting up a fork and all, here is literally the only change we needed for this Workflow. It's only the script that needs adjusting, here is the fully working script as suggested by @cands.

@Subject22 I hope this makes it easy for you to update everything.

 

# Quit Alfred 4 and his preferences
if application id "com.runningwithcrayons.Alfred" is running then
	tell application id "com.runningwithcrayons.Alfred" to quit
end if

set prefsWasOpen to false
if application "Alfred Preferences" is running then
	set prefsWasOpen to true
	tell application "Alfred Preferences" to quit
end if

delay 1 # Wait for the processes to terminate

# Restart and display processes
tell application id "com.runningwithcrayons.Alfred" to activate

if prefsWasOpen then
	tell application "Alfred Preferences" to activate
end if

# Display Alfred
tell application id "com.runningwithcrayons.Alfred" to search

 

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