• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

How to move files using app.system()?

Participant ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

I am trying to move a file from a location to, say, my desktop. How do I do this using app.system()?

 

I found this thread How do I move files through Extendscript? which has started me on the right path, but I don't know AppleScript. I did a bit of searching and put this together, which appears to do nothing (ExtendScript just says Result: 256):

 

app.system("osascript -e 'tell application \"finder\" move POSIX file \"/path/to/file.tif\" to POSIX file \"~/Desktop\" with replacing'");

 

What am I doing wrong here?

TOPICS
Actions and scripting

Views

5.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Dec 09, 2016 Dec 09, 2016

Your script worked fine for me on MacOS Sierra

app.system("mv -f /Users/javier/Desktop/Delete/2016_12_09/image.jpg ~/Desktop");

If you have special characters or spaces, you will need to escape them with a "\\", like this:

app.system("mv -f /Users/javier/Desktop/Delete/2016\\ 12\\ 09/image.jpg ~/Desktop");

One of my folders is called "2016 12 09", so I have to escape those characters --> "2016\\ 12\\ 09"

You can also do it with osascript, like you initial code. You had a couple of errors, you were m

...

Votes

Translate

Translate
Adobe
Advocate ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

There may be more, but the obvious one is that the Finder has to be capitalized:

tell application \"Finder\"…

However, if you are already using app.system, why not just use the simple shell command "mv -f /path/to/file.tif ~/Desktop/" ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

Capitalizing Finder didn't make a difference (the example in the thread I linked had it in lowercase and that code worked).

For the shell command, is it simply something like this:

app.system("mv -f /path/to/file.tif ~/Desktop/")

Because that didn't do anything. ExtendScript just gave me the same 'Result: 256'.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

I do not us a mac here is what I found on the web

javascript - Moving created files with JXA - Stack Overflow

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

Not sure if this is what you're looking for but if you use the File.copy() and File.remove() combination in ESTK you can move files around

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

If on the same volume a rename may be all that is needed. If one can include path in the new name? Man not be abd option in ExtendScript.

Capture.jpg

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

mutagenicpixels​ & JJMack​ - thanks for the suggestions, but I need to actually move the file rather than copy (and then delete) or rename it

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

On some file system you can rename a file on a volume and change its name and its path.  This is a move,  where no file actually is written only file system directires are updated.  A move betweem two volumes requires the file be copied from the original first volume and written to a second volume then the original file is removed from the first volume with a directory update.

The file system command to do that rename may be move in either case.  It depends in the file system.  Mac OSX is a Unix flavored File system like linix has.  I just do not know what Files system commands ExtendsScript supports Moving or Rename File / Directory in Linux - 10 Practical mv Command Examples

You may need to use copy and delete or change path and rename for I see no File.move in Extendscript.

Capture.jpg

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

Yeah, the fact that File doesn't have a move() method (annoyingly) is why I made this thread. Unfortunately, adding a path to rename doesn't work..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

Yeah that is annoying, I've been using the copy/remove combo for all my file sorting needs and it's done me well so far.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

Did you try

File.changePath();

File.rename();

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

Yes, and neither worked. Do you know anything about running shell commands using app.system()? This seems like it should work, but it does nothing..

app.system("mv -f /path/to/file.tif ~/Desktop/") 

Edit: This is working at my computer at home. Strange. I will test it when I'm back at work in the morning.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 18, 2020 May 18, 2020

Copy link to clipboard

Copied

LATEST

Can you demonstrate to me how to use .changePath method, as for me it does not work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

I'm thoroughly confused. Running those commands in terminal works just fine, but when I use app.system() it does nothing. It just keeps saying 'Result: 16384'

maxwyss​, JJMack​, xbytor2​ (sorry to tag..). Do you have any ideas why this might be the case?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

I use windows to use  app.system()  you may need to use different commands depending on platform for a Mac need to use some Unix shell to use a file system command like move.   Extendscript support most OS file system command but not move so if you want to use move you would need to use an external command line command or some type of shell to use move for the approbate OS. I use windows and feel the code I have used with app.system() would only work on a Windows system.  For I use DOS  Start or CMD command to run a command line command or application. I used the following code to run and external application and  waited for the external application to create its output file so the script could use the file to finish its processing. My code also requited the external program the be in the folder my script is in so it is in the current folder.  When I test the existence of the application I did not use a path.

///////////////////////////////////////////////////////////////////////////////

//                   Start external Program                    //

///////////////////////////////////////////////////////////////////////////////

function StartPgm(pth,pgm,opt,file){

  var pgmfile = new File(pgm);

  if(!pgmfile.exists){alert("Program missing 'protrace.exe'\rMust be with script in folder\r" + pth ); return; }

  //alert('app.system(Start "' + pgm + '" /D "' + pth + '" /MIN "' + pgm + '" '  + opt + ' "' + file + '"' );

  app.system('Start "' + pgm + '" /D  "' + pth + '" /MIN "'  + pgm + '" ' + opt + ' "' + file + '"'  );

};

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

This worked for me on windows

var moveFile =  'C:\\TestFolder\\TestFile.txt';

var toFolder =  'E:\\TestFolder1\\';

if(!(new Folder(toFolder)).exists){ new Folder(toFolder).create();}

if(!(new Folder(toFolder)).exists){ alert("Folder " + toFolder + " could not be created.");}

else {

  if(new File(moveFile).exists){

  app.system('Move /Y "' + moveFile + '"  "' + toFolder + '"'  ); //will ovewrite with /Y

  for (var i=0;i<100000;i++) { if (!(new File(moveFile)).exists) break; }  //give it some time

  if(!(new File(moveFile)).exists){alert("File " + moveFile + " Moved to " + toFolder ); }

  }

  else {alert("File " + moveFile + " does not exists");}

}

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

Your script worked fine for me on MacOS Sierra

app.system("mv -f /Users/javier/Desktop/Delete/2016_12_09/image.jpg ~/Desktop");

If you have special characters or spaces, you will need to escape them with a "\\", like this:

app.system("mv -f /Users/javier/Desktop/Delete/2016\\ 12\\ 09/image.jpg ~/Desktop");

One of my folders is called "2016 12 09", so I have to escape those characters --> "2016\\ 12\\ 09"

You can also do it with osascript, like you initial code. You had a couple of errors, you were missing the "to" word before "move", and AppleScript doesn't seem to like the "~" character, so I replaced it with the absolute path.

This line worked for me:

app.system("osascript -e 'tell application \"Finder\" to move POSIX file \"/Users/javier/Desktop/Delete/2016_12_09/image.jpg\" to POSIX file \"/Users/javier/Desktop\" with replacing'");

If that's not the issue, it could be related to a permissions issue.

Hope it helps!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

One of my folders is called "2016 12 09", so I have to escape those characters --> "2016\\ 12\\ 09"

I tend to do this instead:

app.system('mv -f "/Users/javier/Desktop/Delete/2016 12 09/image.jpg" ~/Desktop'); 

in code but use escapes on the command line.

-X

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

Hey xbytor2, I use quotes too, but it didn't work when I tried, so I didn't mention it. And I just realized why it didn't work. I was adding the second path in quotes as well, but it doesn't work if you're using the "~" character...

Definitely use quotes instead of escaping the chars. You will save an extra, and annoying step

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 11, 2016 Dec 11, 2016

Copy link to clipboard

Copied

Thanks for clearing that up for me, Javier! I've been playing around with this at home now and it works great.

If we are able to make shell commands through app.system(), is it possible to use it to get the results back from a grep search of a folder?

var test = app.system('ls "/Users/constantinc/Creative Cloud Files/Main Scripts/Test/Test Images/" | grep 123456')

I was hoping that the results would be stored in test, but only the number 0 was.

Edit: A temporary solution I've made is:

var test = "/Users/constantinc/Creative Cloud Files/Main Scripts/Test/Test Images/"

app.system('ls "' + test + '" | grep 123456 > ~/Desktop/Results.txt')

var resultsFile = File('~/Desktop/Results.txt')

var resultsArray = []

resultsFile.open('r')

resultsArray = resultsFile.read().split('\n')

Also, not sure if I should have made a separate thread for this?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2016 Dec 11, 2016

Copy link to clipboard

Copied

It does not look like you waited some time to give the commands to finish. You may be reading  the results before the file was written. When  move the file I loop for some time to see if the it was not where it was then tested that it was not there and was in the move to location. How do you know the command finished before using its output. The command will run 6independently from the script. 

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 13, 2016 Dec 13, 2016

Copy link to clipboard

Copied

Good point! I could have it always check for and remove the results.txt file before triggering the search, and then have it check if it exists in a loop before reading the file (similar to your script). However, I was wondering about directly storing the results of the app.system() into a variable, rather than having to pipe it into a txt file and then read that. Do you know if that's possible?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 13, 2016 Dec 13, 2016

Copy link to clipboard

Copied

Hey Whatevermajorloser​, I don't know of a way to return the output of the app.system command. I do, however, have an alternative for the specific command you're using 'ls | grep', which (correct me if I'm wrong), will list out the files in a folder and filter them using grep and a string that you specify.

In ExtendScript, you could read the contents of a folder, loop through the files, and have an if statement with indexOf to filter the files that contain the keyword you're looking for.

var myFolder = File('~/Desktop');

var myFiles = myFolder.getFiles();

var filteredFiles = [];

for(var i = 0; i < myFiles.length; i++) {

    if(myFiles.name.indexOf('.txt') !== -1) {

        filteredFiles.push(myFiles.name);

    }

}

Alternatively, you could filter directly from the getFiles function. Both methods will return an array of the filtered elements.

var myFolder = File('~/Desktop');

var myFiles = myFolder.getFiles(/.txt/);

Hope this helps.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 13, 2016 Dec 13, 2016

Copy link to clipboard

Copied

Hi Javier, this is actually something I tried before (see here) and gave up on because it was too slow.

I've just started looking into this again after discovering app.system(). The files that I need to search for are on a network drive (which probably contributed to getFiles() performing so slowly) and, having tested it in terminal, so far using ls combined with grep on the command line is far superior speed wise (almost instantaneous), so I don't mind if I have to do a little work around to get those speeds.

It's a shame you can't just store the results directly into a variable though! That would be amazing.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 10, 2016 Dec 10, 2016

Copy link to clipboard

Copied

This function seems to work on Windows.  I added code for use on a Mac. However, I can not test on a Mac. Jive seems to mess up pasted in formatted text. I tried to fix it somewhat.

// A Move File Test Script

var fileName   = 'TestFile.txt';

var fromFolder = 'C:\\TestFolder\\';

var toFolder   = 'C:\\TestFolder1\\';

var newName    = '';

if (!(moveFile(fromFolder,fileName,toFolder,newName))) alert('Move of "' + fromFolder + fileName + '" to "' + toFolder + newName+ '" Failed' );

//else alert('Move of "' + fromFolder + fileName + '" to "' + toFolder + newName+ '" Successful' );

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function moveFile(fromFolder,fileName,toFolder,newName) {

  retcode = false;

  if(!(new Folder(toFolder)).exists){ new Folder(toFolder).create();}

  if(!(new Folder(toFolder)).exists){ alert("Folder " + toFolder + " could not be created.");}

  else {

      if (new File(fromFolder + fileName).exists){

         if (isWindows){app.system('Move /Y "' + fromFolder + fileName + '"  "' + toFolder + newName + '"'  );} //will overwrite with /Y without prompt

         if (isMac){app.system('mv -f "' + fromFolder + fileName + '"  "' + toFolder + newName + '"'  );} //will overwrite with -f

         for (var i=0;i<100000;i++) { if (!(new File(fromFolder + fileName).exists)) break; } //give it some time

         if (newName=="") newName = fileName;

         if ( !(new File(fromFolder + fileName).exists) & (new File(toFolder + newName).exists) )  retcode = true;

     }

     else {alert("File " + fromFolder + fileName + " does not exists");}

  }

  return retcode;

}

function isWindows() { 

  return app.systemInformation.indexOf("Operating System: Windows") >= 0 

function isMac() { 

  return app.systemInformation.indexOf("Operating System: Mac") >= 0 

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines