sanjihan
sanjihan

Reputation: 5992

move files with AppleScript

I am trying to move a file with AppleScript. This Is the code I use:

on adding folder items to this_folder after receiving these_items
    repeat with i from 1 to number of items in these_items
        set this_item to item i of these_items
        set the item_info to info for this_item
        set the item_path to the quoted form of the POSIX path of this_item

...
...   //irrelevant code here
...


       tell application "Finder"
         move POSIX file item_path to POSIX file "/Users/mainuser/Desktop/Books"
       end tell

    end repeat
end adding folder items to 

If I replace item_path with a regular path such as /Users/mainuser/Desktop/Test/test.png than it works great. What could be the reason for my problem?

Upvotes: 0

Views: 1328

Answers (2)

foo
foo

Reputation: 3259

Use:

on adding folder items to this_folder after receiving these_items
    tell application "Finder" to move these_items to folder "Books" of desktop
end adding folder items to

The after receiving parameter is a list of AppleScript alias values, which is something Finder's move command already understands and knows how to work with. While it's unusual for application commands like move to accept values that aren't references to application objects, it's not completely unknown; particularly with pre-OS X apps like Finder whose scripting support was completely hand-written, allowing developers to make it work in ways that would be helpful to users and not just in ways dictated by dumb, standardized frameworks like OS X's Cocoa Scripting.

Upvotes: 1

vadian
vadian

Reputation: 285059

these_items is an array of alias specifiers, any further coercion is not needed

tell application "Finder"
     move this_item to folder "Books" of desktop
end tell

the property desktop points always to the desktop of the current user.

By the way: The Finder doesn't accept POSIX paths (slash separated), only the native HFS paths (colon separated).

PS: The reason why item_path does not work is the quotation.
It's only needed in do shell script

Upvotes: 2

Related Questions