Jump to content

Syncing iCloud Text Shortcuts to Snippets


Recommended Posts

Hi everyone,

 

I'm already using many text shortcuts that you can set on your Mac or iPhone via System Preferences -> Keyboard -> Text. It's great because it syncs to all of your devices (something Alfred doesn't do since it's not made for phones). However, the shortcuts don't work everywhere (e.g. address bar in Chrome) and they don't have the immediate expansion that Alfred has (you have to press spacebar or select it).

 

Do you think there would be a way to sync the iCloud text shortcuts to a snippet collection?

 

All the best,

Henrik

Link to comment
11 minutes ago, henrik said:

something Alfred doesn't do since it's not made for phones

 

That's not the reason. It's not possible to build a snippet expansion app for iOS due to the way the system works.

 

12 minutes ago, henrik said:

Do you think there would be a way to sync the iCloud text shortcuts to a snippet collection?

 

It might be possible. Do you know where the system snippets are stored?

Link to comment

Thanks for the quick reply!

 

23 minutes ago, deanishe said:

It might be possible. Do you know where the system snippets are stored?

 

They are stored under /Users/henrik/Library/KeyboardServices/TextReplacements.db. Does that help?

Link to comment
  • 1 month later...

Sorry @henrik, I missed your reply.

 

It isn’t a workflow script, it’s a command-line script (I figured most people would want to run it automatically via a Launch Agent).

 

If you read the post, you’ll see you have to set the SNIPPET_DIR environment variable to the path to your Alfred snippets directory. You can also set SNIPPET_DIR as a workflow variable, but if you don’t set it, the snippets will just be saved in the workflow folder, which is useless. If you're running the script from a workflow, you can probably set SNIPPET_DIR to ../../snippets.

 

Link to comment
  • 3 months later...
3 hours ago, paulw said:

Do you think it would be feasible to make it a two-way sync

 

No. Changing the SQLite database is a bad idea:

 

Quote

Although Core Data supports SQLite as a store type, the store format—like those of the other native Core Data stores—is private. You cannot create a SQLite database using the native SQLite API and use it directly with Core Data, nor should you manipulate an existing Core Data SQLite store using native SQLite API.

 

 

Link to comment
  • 9 months later...

Thanks! By the way, I did a file comparison between the old and new versions, and you changed export_shortcuts to export_shorcuts. Doesn't seem to be a problem, since it's consistent, but just thought I'd let you know 🤓 Never mind, had that completely backwards. Thanks for your very useful script!

Edited by paulw
Link to comment
  • 1 month later...
On 12/13/2020 at 11:08 AM, paulw said:

since I do have Hazel.

@paulw Did you get to using hazel to auto sync this when a new snippet is added? Would it be possible to share your hazel rule? I couldn't get it to work

 

EDIT: was also thinking it there's a way to backup existing snippets before overwriting them in alfred

Edited by andy4222
Link to comment
27 minutes ago, andy4222 said:

Is there a way to get confirmation from the script that it successfully ran so we can post notification about the success or error?

 

It's a standard POSIX script. If it failed, it exits with a non-zero status, and it logs what it's doing to STDERR. Wire it up however you want.

Link to comment
21 minutes ago, deanishe said:

 

It's a standard POSIX script. If it failed, it exits with a non-zero status, and it logs what it's doing to STDERR. Wire it up however you want.

 

I tried putting in the invalid directory path and it didn't log an error. How do I fetch/return stderr from the script

 

PS: not a developer, just play with this stuff

 

 

Link to comment
1 minute ago, andy4222 said:

the invalid directory path

 

What does that mean? A path that isn't your snippets folder or an actually invalid path (illegal name, directory you can't write to, etc.)?

 

The script doesn't care about the former. If it can write to the path you give it, it will. It's your job to make sure it's pointing at your snippets directory.

 

4 minutes ago, andy4222 said:

How do I fetch/return stderr from the script

 

That depends on how you're running the script, which you haven't told us.

Link to comment
7 minutes ago, deanishe said:

 

What does that mean? A path that isn't your snippets folder or an actually invalid path (illegal name, directory you can't write to, etc.)?

 

The script doesn't care about the former. If it can write to the path you give it, it will. It's your job to make sure it's pointing at your snippets directory.

 

 

That depends on how you're running the script, which you haven't told us.

 

 

SNIPPET_DIR = '<setting wrong path here'

 

7 minutes ago, deanishe said:

 

What does that mean? A path that isn't your snippets folder or an actually invalid path (illegal name, directory you can't write to, etc.)?

 

The script doesn't care about the former. If it can write to the path you give it, it will. It's your job to make sure it's pointing at your snippets directory.

 

 

That depends on how you're running the script, which you haven't told us.

 

My bad. I am using run script for this. Run script is just a copy paste of your script with paths changed to my alfred's snippet directory

 

 

1705217265_ScreenShot2021-11-26at2_28_14AM.png.2d06a08fc1ae43f7595a70be5296a975.png

Edited by andy4222
Link to comment
13 minutes ago, andy4222 said:
SNIPPET_DIR = '<setting wrong path here'

 

That’s a perfectly valid path. Your workflow directory presumably now contains a directory called <setting wrong path here full of snippets.

 

13 minutes ago, andy4222 said:

Run script is just a copy paste of your script with paths changed to my alfred's snippet directory

 

Then use Alfred's debugger to see what the script is doing, like with any other workflow.

 

Alfred doesn't care if a Run Script fails or not, only whether its output is valid, so if you want your workflow to behave differently if the script fails, you'll need to call the Python script from a bash wrapper or similar.

Edited by deanishe
Link to comment
  • 5 months later...
  • 3 weeks later...
On 8/21/2020 at 11:31 PM, deanishe said:

Sorry @henrik, I missed your reply.

 

It isn’t a workflow script, it’s a command-line script (I figured most people would want to run it automatically via a Launch Agent).

 

If you read the post, you’ll see you have to set the SNIPPET_DIR environment variable to the path to your Alfred snippets directory. You can also set SNIPPET_DIR as a workflow variable, but if you don’t set it, the snippets will just be saved in the workflow folder, which is useless. If you're running the script from a workflow, you can probably set SNIPPET_DIR to ../../snippets.

 

@deanishe thanks for script to copy macOS text shortcuts to Snippets. How we do set environment variables above? In python code, 

 

def export_shortcuts(shortcuts, dirpath):
    """Save macOS shortcuts to directory as Alfred snippets."""
    log('exporting snippets to %r ...', dirpath)
    if not os.path.exists(dirpath):
        os.makedirs(dirpath, 0700)

 

what does 0700 means in the line above? Is it possible to use AppleScript instead of Python to achieve this one way sync?

 

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