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!
Best Answer
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.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 theif
statement runs in the script.You can either manually set the
com.TestService.flag
usingxattr
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 to1
as it reads the emptyflag
variable the first time as being equal to0
.The following output from Terminal shows the extended attributes of the Automator's Service
.workflow
file when it was first saved, it just had thecom.apple.FinderInfo
attribute set, and then the Service was run a couple of times to show that thecom.TestService.flag
attribute was set and changed each time the Service was run. (I manually ran thexattr
command before and then in-between each time I ran the Service to have this output to show.)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 theawk
command and in eachxattr
command in each branch of theif
statement. Also change the name of thefile
variable as appropriate for the actual name of your Automator Service.workflow
file.