4

I went to run an old script and it broke after the 10.9 update. I used to move files with system events with the following code.

set Somefilepath to "Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt"
set somefolderpath to "Design_005_HD:Users:Design_005:Desktop:End:"

tell application "System Events"
move file (Somefilepath) to folder (somefolderpath)
end tell

Now it gives me the following error.

error "System Events got an error: Can’t make file \"Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt\" into type integer." number -1700 from file "Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt" to integer

I know I can swap it out and use finder but I rather not use it. What changed that is no longer works?

Update 4/2/14

I have tried this in every way of giving the file/folder location to system events and it doesn't work. I am glad it is just not me who cannot get it to work. I will update this post if I find an answer or a working update is made.

Update 4/3/14

It seems this is just a bug that system events can't move files. I have reported it here http://bugreport.apple.com/ and everyone else should too.

Please do not take my code to heart, it is just where things ended up when I couldn't get it to work. I have working code for 10.8.5 and it is what is shown above minus the folder tag in the system events tell block. No idea why it works with out the folder tag but it does. Tested on multiple comps. If it isn't broken don't fuss over it. Noted it and moved on.

Update 10/20/14

For anyone interested. I have received an e-mail stating my ticket has been closed/resolved. So mavericks will for ever be broken but there might be light for Yosemite when it comes out.

Greg
  • 9,068
  • 6
  • 49
  • 91
Tim Joe
  • 475
  • 8
  • 18
  • Just use the `"Finder"`context instead of the "System Events" context to work around the problem. In a comment you say, " I was avoiding the Finder because it seems to be slower and I don't know much of shell scripts to use them." - not sure that using Finder is slower, though I haven't test - startup cost is not an issue, because Finder is always running. You can also limit use of Finder to the move operation. – mklement0 Apr 02 '14 at 13:45
  • I've updated my answer to show you a workaround both for HFS-style (colon-based) paths and POSIX-style paths (as in the original version of your question). In the future I encourage you not to revise your questions too drastically without comment, as that may (partially) invalidate some of the already-given answers and leave future visitors puzzled. I've submitted a bug report to Apple at http://bugreport.apple.com, and I encourage you to do the same (sadly, bug reports are not public, but the more people tell them about a given bug, the more likely they are to take notice.) – mklement0 Apr 02 '14 at 17:51
  • To be clear: the bug is in 10.8 _too_, you just found a "loophole" in omitting the `folder` type specifier; the point is: it _should_ work with the `folder` type specifier and using that specifier is the _proper_ way to write that command. Using `tell application "Finder"` instead works on both 10.8 and 10.9, and is generally the way to go for file-system _manipulation_ tasks. – mklement0 Apr 03 '14 at 22:44
  • Update: Using `Finder` is indeed in general much slower than using `System Events`, especially when _iterating_ over items in a folder; that said, in the case at hand the difference will be negligible. – mklement0 Apr 15 '14 at 06:07

3 Answers3

3

In general, Applescript works with colon delimited paths (:) not slash delimited paths (/). I say in general because some applications will work with slashes but all programs will work with colons. For an example of what the colon paths look like try this code and look at the result...

set colonPath to (path to desktop as text) & "untitled folder 2:"

So first I would convert your slashes to the colon style.

Also to applescript these are just strings not paths. To make applescript understand they are paths we can do a few things. First you can add the words file or folder in front of them as appropriate. I notice in your code you are using "file" in front of the file string but you aren't using "folder" in front of the folder string. So try that. Second you can just use "alias" in front of the strings whether they're files or folders. There are other ways as well but I'll stop here. Either of those ways should work.

UPDATE: with all of the above being said, it seems System Events in 10.9 still has trouble with the move command. As such here's 2 alternatives for you. I used slash style paths since that's what you're using.

set somefilepath to POSIX file "/Users/Design_005/Desktop/Start/TextFile.txt"
set somefolderpath to POSIX file "/Users/Design_005/Desktop/End"

tell application "Finder"
    move somefilepath to somefolderpath
end tell

or

set somefilepath to "/Users/Design_005/Desktop/Start/TextFile.txt"
set somefolderpath to "/Users/Design_005/Desktop/End"

do shell script "mv " & quoted form of somefilepath & space & quoted form of somefolderpath

Good luck.

regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Thank you for the response. I understand what you have said and normally use Colons. on OSX 10.8, my script worked and now on OSX 10.9 it does not. To show it with the current concern I have re-compiled it and updated the post. – Tim Joe Apr 01 '14 at 20:20
  • +1 for the background info; to give things a name: "colon paths" = HFS-style paths; "slash paths" = POSIX-style paths. – mklement0 Apr 01 '14 at 20:48
  • Tim, you've got lots of problems with your code. However System Events is having trouble on 10.9 with the move command even when I fix the errors. As such see the UPDATE to my post for 2 alternatives and error free code. – regulus6633 Apr 01 '14 at 22:45
  • Thanks for the help! I always thought using POSIX file converts the string to a Colon formatted version. Way back when I first start I tested it and got that format and used it as a converter. I wanted to use just "file" and not "POSIX File" and did seem that got left on the post. The slashes where used in this example because I can easily drag a folder into the editor and have it produce the string. I was avoiding the finder because it seems to be slower and I don't know much of shell scripts to use them. – Tim Joe Apr 01 '14 at 22:54
  • @TimJoe: `POSIX file` returns a `file` OBJECT, not an HFS-style (with `:` as the separator) string. If you then - implicitly or explicitly via `as text` - convert this OBJECT into a string, you get the HFS-style path. (You can also get the POSIX-style path back from the object by using `POSIX path of ...` - curiously, `path of ...` does NOT work to get the HFS-style path - use `... as text`). Also note that even though your code isn't the clearest conceptually, there's _nothing wrong with your code per se_ - it's an AppleScript bug. – mklement0 Apr 02 '14 at 13:39
1

You can’t do that. System Events can delete and open, but not move. Here’s a simple example in case it helps someone else find a better answer in a future OS. System Events appears to treat move differently than delete and open.

tell application "System Events"
    set myFile to file "Macintosh HD:Users:velma:Desktop:Test.png"
    set myFolder to folder "Macintosh HD:Users:velma:Desktop:Test"

    --delete works! with both type “file/folder” and type “disk item”
    --delete myFile
    --delete myFolder

    --open works!
    open myFile
    open myFolder

    --move fails!
    move myFile to myFolder
end tell

The error it’s returning, in this case, is “Can’t get file”, number -1728.

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
  • 1
    I suspect a bug; if you put your code into the `"Finder`" context instead, it works (as in my answer). – mklement0 Apr 01 '14 at 22:26
  • @ Jerry Stralton I normally use "set" for declaring paths/strings. Is there a benefit to using "copy" instead? Other wise we had the same line of thinking – Tim Joe Apr 02 '14 at 13:34
  • @TimJoe: The difference between `copy` and `set` only matters with _mutable_ objects (`date`, `list`, `record`, and `script` objects), so in this case they're the same. In general, unless you really want to _copy data_ (duplicate a value), I recommend using `set` - see https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_variables.html#//apple_ref/doc/uid/TP40000983-CH223-SW9 – mklement0 Apr 02 '14 at 14:35
  • @TimJoe and mklement0, using “copy” instead of “set” was the result of my trying several different things and ending up on the “copy” iteration when I realized I wasn’t going to solve the original question. I’ve updated the example so as not to add confusion. – Jerry Stratton Apr 03 '14 at 00:23
  • Not an issue for me, I would have used "set" — just didn't know if there was a reason to use "copy" I didn't know. I have tried everything I could think of/find and understand the "last attempt" code. :P – Tim Joe Apr 03 '14 at 13:24
  • This is not an answer... It's essentially saying "I don't know how to fix your error, but here's how to get a different error!" – Ky - Oct 30 '15 at 13:58
  • @BenC.R.Leggiero My intent was to say “you can’t do that” and provide simplified code that shows it more clearly and provides a starting point for testing in the future. I apologize for the Scooby Doo joke that muddled my answer and have removed it. (I’m assuming that “you can’t do that” is a potentially valid answer, assuming it is correct.) – Jerry Stratton Oct 31 '15 at 01:14
0

There appears to be bug in the move command in the "System Events" context in OX 10.9 (and possibly 10.8).

The workaround is the to use the "Finder" context instead:

Using HFS-style paths (separator is :)

set somefilepath to "Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt"
set somefolderpath to "Design_005_HD:Users:Design_005:Desktop:End:"

tell application "Finder"
    move file somefilepath to folder somefolderpath
end tell

Using POSIX-style paths (separator is /) - as in the original question

set somefilepath to "/Users/Design_005/Desktop/Start/TextFile.txt"
set somefolderpath to "/Users/Design_005/Desktop/End"

# Note that we use `as POSIX file` even in the case of the *folder*
# - this works, however.
tell application "Finder"
    move somefilepath as POSIX file to somefolderpath as POSIX file
end tell

Note:

  • as POSIX file returns a file object in both cases, but Finder still handles the move properly - it is fine to use POSIX file with both files and folders.
  • Note that using the prefix form of POSIX file- e.g., POSIX file "/Library", only works with a path string literal; if you try to build the path string as an expression, it breaks (in the "Finder" context, but NOT in the AppleScript context(!)): POSIX file ("/" & "Library") - by contrast, "/" & "Library" as POSIX file works (in both contexts) - go figure. To be safe, always use the postfix form: ... as POSIX file
  • A downside of using as POSIX file - at least as of OS X 10.9 - is that the error messages are cryptic if a file/folder doesn't exist: you'll see Finder got an error: Handler can’t handle objects of this class. and Finder got an error: AppleEvent handler failed. - both with number -10000.
  • (Using folder directly with a POSIX path, as in an earlier version of the question - e.g., folder "/Library" - ONLY works in a "System Events" context, and is therefore NOT an option in the "Finder" context.)

As for what changed in AppleScript OS X 10.9:

The behavior you see appears to be a bug (also see @Jerry Stratton's answer); nothing in the AppleScript release notes for 10.9 indicates a change in that area.

I now think that the problem affects OS X 10.8 as well.

I encourage you to submit a bug report to Apple at http://bugreport.apple.com, as I already have.

Sadly, handling files, folders, and aliases in AppleScript has always been a mess, with confusion stemming from classes of the same name from different dictionaries (AppleScript itself, System Events, Finder) with subtly different behavior.

A general recommendation: for file-system manipulation, use the tell application "Finder" context.

The "System Events" dictionary, in its Disk-Folder-File Suite, duplicates some of Finder's file-system manipulation functionality, but only some - a curious omission is a file copy command, for instance.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I had no issues in 10.8, or if there was I still got it to work as intended. – Tim Joe Apr 03 '14 at 15:02
  • @TimJoe: Try again on 10.8 with the current code from your question. When I do, on 10.8.5, I get `System Events got an error: Can’t get file \"\"." number -1728 from file ""` - just as I do on 10.9.2.; note that the error message and number differ from the one you report in your question. – mklement0 Apr 03 '14 at 20:11
  • To be honest this was a post to see why my script no longer like system events moving files. @mklement0 I use something that looks like this— ¬set Somefilepath to ((path to desktop) as string) & "Start:TextFile.txt" ¬set somefolderpath to ((path to desktop) as string) & "End:" ¬ ¬tell application "System Events" ¬ move file (Somefilepath) to (somefolderpath) ¬end tell no idea why i need to remove the term folder but it works on 10.8.5. adding folder in will give me error 1728 – Tim Joe Apr 03 '14 at 20:42