Porous6937 Posted May 6, 2023 Posted May 6, 2023 Hi there! I am trying to create a workflow in Alfred that search a keyword and returns a relevant photo from unsplash using the unsplash api. #!/usr/bin/env bash # Set Unsplash API key export UNSPLASH_ACCESS_KEY="enter your unsplash key" # Get query from Alfred query="$1" # URL encode query query="$(echo "$query" | python -c 'import urllib.parse; print(urllib.parse.quote(input()))')" # Search for images on Unsplash using the Unsplash API response=$(curl --silent "https://api.unsplash.com/search/photos?query=$query&client_id=$UNSPLASH_ACCESS_KEY") # Parse the JSON response to get the URLs of the first 10 images urls=$(echo "$response" | jq -r '.results[0:10] | .[].urls.regular') # Output the URLs as JSON for Alfred echo "$urls" | jq -R -s 'split("\n") | map(select(length > 0)) | map({title: ., arg: .})' The workflow doesn't run as expected. Does anyone know what may be wrong?
Stephen_C Posted May 6, 2023 Posted May 6, 2023 Open the debugger, run the workflow and copy and paste the output of the debugger into a post here. Stephen
Terminal Posted May 6, 2023 Posted May 6, 2023 7 hours ago, Porous6937 said: Hi there! I am trying to create a workflow in Alfred that search a keyword and returns a relevant photo from unsplash using the unsplash api. #!/usr/bin/env bash # Set Unsplash API key export UNSPLASH_ACCESS_KEY="enter your unsplash key" # Get query from Alfred query="$1" # URL encode query query="$(echo "$query" | python -c 'import urllib.parse; print(urllib.parse.quote(input()))')" # Search for images on Unsplash using the Unsplash API response=$(curl --silent "https://api.unsplash.com/search/photos?query=$query&client_id=$UNSPLASH_ACCESS_KEY") # Parse the JSON response to get the URLs of the first 10 images urls=$(echo "$response" | jq -r '.results[0:10] | .[].urls.regular') # Output the URLs as JSON for Alfred echo "$urls" | jq -R -s 'split("\n") | map(select(length > 0)) | map({title: ., arg: .})' The workflow doesn't run as expected. Does anyone know what may be wrong? if you are already using using python, why not write it all in python and avoid the bash python mix.
vitor Posted May 6, 2023 Posted May 6, 2023 42 minutes ago, Terminal said: if you are already using using python, why not write it all in python and avoid the bash python mix. Perhaps they’re not familiar with Python in general, but found that snippet online to do just that part of the task. Note how a non-standard but popular tool (jq) is used as well. That did make me notice something. @Porous6937 you’re using python, which no longer ships with macOS (only python3), which may be the cause of the problem. Try replacing: # Get query from Alfred query="$1" # URL encode query query="$(echo "$query" | python -c 'import urllib.parse; print(urllib.parse.quote(input()))')" With: # URL encode query from Alfred osascript -l JavaScript -e 'function run(argv) { return encodeURIComponent(argv[0]) }' "${1}" If that doesn’t work, follow @Stephen_C’s suggestion and show us the output from the debugger.
Porous6937 Posted May 8, 2023 Author Posted May 8, 2023 Thank you for the quick help guys! @Stephen_C you are right that's what I should 've done from the beginning! Not good at coding though. The output is the following Quote [16:29:16.289] Unsplash Image Search[Script Filter] Queuing argument 'mountain' [16:29:16.576] Unsplash Image Search[Script Filter] Script with argv '(null)' finished [16:29:16.597] STDERR: Unsplash Image Search[Script Filter] Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named parse [16:29:16.598] Unsplash Image Search[Script Filter] [] [16:29:17.027] Unsplash Image Search[Script Filter] Script with argv '(null)' finished [16:29:17.049] STDERR: Unsplash Image Search[Script Filter] Traceback (most recent call last): @Terminal Thanks for the guidance but I am not familiar with coding in any language @vitor You got me! I am not familiar with coding and I asked chatGPT. 🥴 I have installed python 3 and all other projects I have work with python. I tried replacing the code with the one you wrote but didn't have any success. I'll guess I'll try the python only way... Thank you all for your tips and thoughts! You have a very nice community here!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now