Shortcut to write to a file

I'm getting variable and frustrating results with a very simple task. I created a shortcut to pass a text variable to the Append to Text File function to write the result to a file. The default location is 'Shortcuts' folder at the top level of iCloud Drive (see image). If I use 'Desktop' as the location, the file is not written to the Desktop folder, but instead reverts back and creates a file in the Shortcuts folder. If I use 'Documents' as the location, I get an error stating I don't have permission. If I use a personal folder I create at the top level within iCloud Drive, the file is written to the correct location. Why the different behaviors? I would prefer to write the file to a subfolder in 'Documents' which makes the most sense.


MacBook Pro (M2, 2022)

Posted on Nov 19, 2023 4:10 PM

Reply

Similar questions

2 replies

Nov 19, 2023 5:47 PM in response to codad

I tend to find Shortcuts and AppleScript somewhat frustrating, out past the sorts of tasks and GUI scripting those can do...


Here's an overly-chatty bash shell script that shows file creation, a test for writability, appending, and then overwriting:


#!/usr/bin/env bash
set -e
file="/Users/mrhoffman/Documents/testingfile.tmp"
if [ ! -e "$file" ] ; then
    touch "$file"
fi
printf "\n$file contains:\n"
cat "$file"
if [ ! -w "$file" ] ; then
    printf "Unable to write to: $file\n"
    exit 1
fi
printf "Hello, this is a test\n" >> "$file"
printf "Hello, this is another test\n" >> "$file"
printf "\n$file now contains:\n"
cat "$file"
printf "\n$file now contains:\n"
printf "Hello, this is the overwrite\n" > "$file"
cat "$file"
printf "\ndone\n"


To use that example script, launch Terminal.app and create a plain text file at the command line with that code, say with pico x.sh or such, and then (once) chmod u+x x.sh to permit the script to execute, then ./x.sh to execute it.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Shortcut to write to a file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.