Jump to content

[SOLVED] AppleScript that creates reminder freezes Alfred for a long time


Recommended Posts

I've created a workflow that adds a reminder to a pre-determined list in Reminders. E.g. if I type "buy Apples" into Alfred, it adds the item "Apples" to the list called "shopping list" in Reminders. The workflow uses an AppleScript to create the reminder.

 

The workflow looks like this: "Keyword > Run AppleScript > Post Notification"

 

This is the AppleScript:

on alfred_script(q)
	tell application "Reminders"
		tell list "Einkauf"
			set newremin to make new reminder
			set name of newremin to q
		end tell
	end tell
end alfred_script

If I run this script by itself in the Script Editor, it takes 2 to 3 seconds to execute. However, if I run the workflow, it takes about 40 seconds before the Notification at the end appears. During this time neither Alfred nor Reminders will respond.

 

Is there a way to speed up this process? Maybe there's a better way to execute scripts from within a workflow? It could be that the fault is entirely on Reminders' side; in which case I'd love to know if a workaround exists.

Link to comment
2 hours ago, deanishe said:

Run NSAppleScript is executed on Alfred's main thread, which is why it's blocking.

 

That's the kind of thing I was hoping for. Excellent solution!

 

I configured it like this, with the "running instances: concurrently" being an important setting.

 

image.thumb.png.922ebb2a935240195ec7342dd29c2629.png

 

It still takes a large amount of time for the reminder to appear, but since Alfred remains responsive, I can continue to work immediately, and even fire off the next "buy ..." command, which is what's important to me.

Link to comment
  • vitor changed the title to [SOLVED] AppleScript that creates reminder freezes Alfred for a long time
On 5/12/2020 at 2:25 PM, waldrumpus said:

It still takes a large amount of time for the reminder to appear

 

It's very strange that it takes so long. Communicating with applications via AppleScript is certainly slow, but normally no more than two seconds, not 30+.

Link to comment
On 5/12/2020 at 11:56 AM, deanishe said:

Run NSAppleScript is executed on Alfred's main thread, which is why it's blocking.

 

I always wondered why would one run AppleScript on the main thread if `Run Script action with Language = /usr/bin/osascript (AS) instead` exists?

Link to comment
9 hours ago, deanishe said:

 

It's very strange that it takes so long. Communicating with applications via AppleScript is certainly slow, but normally no more than two seconds, not 30+.

 

I timed some consecutive executions; it takes 37 seconds on average until the Reminders app becomes responsive again and Alfred presents the notification at the end of the workflow. If I run the same script from the Script Editor, it takes about 3 seconds.

 

I assume the delay is located in the Reminders app, but I have no idea how to diagnose this further.

Edited by waldrumpus
Link to comment
6 hours ago, nikivi said:

 

I always wondered why would one run AppleScript on the main thread if `Run Script action with Language = /usr/bin/osascript (AS) instead` exists?

 

In my case it's because this is my first Alfred workflow and I'm just getting into things 😅

 

I was in fact working on writing a shell script that would execute the AppleScript when deanishe's reply arrived. The fact that the "Run Script" action has a setting for running osascript, and concurrently to boot, is of course fantastic.

Link to comment
8 hours ago, nikivi said:

I always wondered why would one run AppleScript on the main thread if `Run Script action with Language = /usr/bin/osascript (AS) instead` exists?

 

Because it looks like the right way to run an AppleScript if you don't already know it isn't. And sometimes, you want to run code within Alfred (e.g. calling display notification in Run NSAppleScript shows Alfred’s icon; calling it from a Run Script shows Script Editor’s icon).

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