Moving files from Desktop to another folder and vice versa using AppleScript (and bash inside of Applescript) inside Automator I need to move files from my desktop to a folder ~/Documents/Desktop migration and I have tried to code a script which will do that. Unfortunately, (and the reason why I'm asking here) it doesn't work.

I am using a service (so that I can go to any application's menu bar > Services > (insert name of service here) ) and need to store the variable data, so I think automator is suited for the job, and I'm using AppleScript and Terminal to move. 

What doesn't work is it only transfers files to the desktop from the Desktop Migration folder and not back from the desktop to the Desktop Migration folder, so how can I achieve this?

What I've come up with so far:


on run {input, parameters}
    set theQuery to input
    if theQuery = 1 then
        do shell script "mv ~/Desktop/* ~/Documents/Desktop\\ migration"
        set output to 0
        return output
    else if theQuery = 0 then
        do shell script "mv ~/Documents/Desktop\\ migration/* ~/Desktop"
        set output to 1
        return output
    end if
end run


Thanks!
So it looks like you'er trying to set a flag that toggles each time the Service runs, and there is more than one way to accomplish this.

In this example, I created an Automator Service named Test Service, with the settings shown in the image below, and added a Run Shell Script action, as this is all you should need to accomplish the task since you were using a do shell script AppleScript command anyway to do the work.



As presently coded, each time the Service runs it says what the flag is set to, 0 or 1, and sets it to the opposite. You'd of course remove the say... commands replacing them with the code you want to run based on the setting of the flag.

#!/bin/bash

file="$HOME/Library/Services/Test Service.workflow"

flag="$(xattr -l "$file" | awk '/com.TestService.flag/{print $2}')"

if [[ $flag -eq 0 ]]; then
    # Do something...

    say "flag is currently set to 0, setting flag to 1"

        # Set flag to 1.
    xattr -w com.TestService.flag 1 "$file"
else
    # Do something else...

    say "flag is currently set to 1, setting flag to 0"

        # Set flag to 0.
    xattr -w com.TestService.flag 0 "$file"
fi




This uses xattr to set an extended attribute on the Automator's Service .workflow file and it is this that is read each time and changed each time the Service is run and this is the flag to determine which branch of the if statement runs in the script.

You can either manually set the com.TestService.flag using xattr in Terminal before you run the saved Automator's Service so as to have the extended attribute applied to is .workflow file or if you just run the saved Automator's Service the first time is will set it to 1 as it reads the empty flag variable the first time as being equal to 0.

The following output from Terminal shows the extended attributes of the Automator's Service .workflow file when it was first saved, it just had the com.apple.FinderInfo attribute set, and then the Service was run a couple of times to show that the com.TestService.flag attribute was set and changed each time the Service was run. (I manually ran the xattr command before and then in-between each time I ran the Service to have this output to show.)



$ xattr -l "$HOME/Library/Services/Test Service.workflow"
com.apple.FinderInfo:
00000000  00 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000020
$ xattr -l "$HOME/Library/Services/Test Service.workflow"
com.apple.FinderInfo:
00000000  00 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000020
com.TestService.flag: 1
$ xattr -l "$HOME/Library/Services/Test Service.workflow"
com.apple.FinderInfo:
00000000  00 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000020
com.TestService.flag: 0
$ 




Obviously you can change the name of the flag from com.TestService.flag to whatever you'd like however remember to change it in the script in all three places, in the awk command and in each xattr command in each branch of the if statement. Also change the name of the file variable as appropriate for the actual name of your Automator Service .workflow file.