|
|
#1 |
|
Triple-A Player
Join Date: Jan 2006
Posts: 128
|
AppleScript: Move files into new folder???
I'm trying to write an applescript that will allow me to select a group of files, and move them into a new folder (named with a dialog box) in the same folder as the files (frontmost folder)..... I'm a real Applescript newbie - this script is a mixture of bits I've put together from googling.....
It won't work - works up until it tries to get the files..... then fails with this error - Finder got an error: Can't get item 1 of {document file "jaune-flammee.jpg" of folder "lee" of folder "Users" of startup disk}..... This is the script: -------------------------------------------------- try tell application "Finder" to set the this_folder to (folder of the front window) as alias on error -- no open windows set the this_folder to path to desktop folder as alias end try tell application "Finder" set selected_items to the selection set thefoldername to text returned of (display dialog "Folder name:" default answer "new folder") make new folder at this_folder with properties {name:thefoldername} repeat with x in selected_items move x to thefoldername end repeat end tell --------------------------------------------------- I've tried all sorts of ways to word that last line.... I'm stuck! Can anyone help???
__________________
cheers Lee 15" MacBook Pro 2.66GHz i7 Gone to pasture: 1400c Powerbook, G3 CRT iMac, G4 iBook 800MHz, G4 iBook 1.33GHz, MacBook 2.16GHz |
|
|
|
|
|
#2 |
|
All Star
Join Date: Dec 2005
Location: Plymouth, Massachusetts
Posts: 630
|
The problem is in this line:
Code:
move x to thefoldername Code:
try
tell application "Finder" to set the this_folder to (folder of the front window) as alias
on error -- no open windows
set the this_folder to path to desktop folder as alias
end try
tell application "Finder"
set selected_items to selection
set thefoldername to text returned of (display dialog "Folder name:" default answer "new folder")
set theFolder to (make new folder at this_folder with properties {name:thefoldername}) --added "set theFolder to.."
repeat with x in selected_items
move x to theFolder --changed the variable from "thefoldername" to "theFolder"
end repeat
end tell
|
|
|
|
|
|
#3 |
|
Triple-A Player
Join Date: Jan 2006
Posts: 128
|
Cheers!
Thank-you very much!
I wonder why applescript would think I would want to make a new folder, then move files to the name of it, rather than the folder itself???
__________________
cheers Lee 15" MacBook Pro 2.66GHz i7 Gone to pasture: 1400c Powerbook, G3 CRT iMac, G4 iBook 800MHz, G4 iBook 1.33GHz, MacBook 2.16GHz |
|
|
|
|
|
#4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
All Star
Join Date: Dec 2005
Location: Plymouth, Massachusetts
Posts: 630
|
The short anwer is that is what you told it to do. As a novice myself, I may not be the best person to provide an explanation, but I'll give it a shot.A file can't be moved unless there is a new location provided. The variable "thefoldername" is a string - it gives the script name, not the location, of the folder. After the folder is created, that variable still refers to the name of the folder. Your script needed a Finder reference (file "File" of folder "Folder" of folder "Users" of startup disk) to provide the location of the folder. The event log is very useful for understanding what is happening when a script runs. The original location of the file was already provided by the line Code:
set selected_items to selection
This is when the folder was created (an alias is another way to refer to the location of, or path to, a file or folder)
and the result - a Finder reference - is shown next
I hope that was understandable. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#5 |
|
Triple-A Player
Join Date: Jan 2006
Posts: 128
|
Clear.... I suppose the folder name was chosen in a dialog, not a choose folder dialog, wasn't it.
__________________
cheers Lee 15" MacBook Pro 2.66GHz i7 Gone to pasture: 1400c Powerbook, G3 CRT iMac, G4 iBook 800MHz, G4 iBook 1.33GHz, MacBook 2.16GHz |
|
|
|
|
|
#6 |
|
All Star
Join Date: Dec 2005
Location: Plymouth, Massachusetts
Posts: 630
|
This line
set thefoldername to text returned of (display dialog "Folder name:" default answer "new folder") is where the folder name comes from. After this point, the script will replace "thefoldername" with whatever name you entered, or "new folder" if you entered nothing. Before the script is run, the folder name is unknown. The variable "thefoldername" is a placeholder for the unknown name. Your script doesn't have a choose folder dialog, but could be rewritten to include one if you wanted the option of an existing folder: Code:
try
tell application "Finder" to set the this_folder to (folder of the front window) as alias
on error -- no open windows
set the this_folder to path to desktop folder as alias
end try
tell application "Finder"
set selected_items to selection
set folderType to button returned of (display dialog "Would you like to create a folder or use an existing one?" buttons {"New", "Existing", "Cancel"} default button "New")
if folderType is "New" then
set thefoldername to text returned of (display dialog "Folder name:" default answer "new folder")
set theFolder to (make new folder at this_folder with properties {name:thefoldername})
else
set theFolder to choose folder
end if
repeat with x in selected_items
move x to theFolder
end repeat
end tell
|
|
|
|
|
|
#7 |
|
Triple-A Player
Join Date: Jan 2006
Posts: 128
|
Cool - thanks for the tip!
__________________
cheers Lee 15" MacBook Pro 2.66GHz i7 Gone to pasture: 1400c Powerbook, G3 CRT iMac, G4 iBook 800MHz, G4 iBook 1.33GHz, MacBook 2.16GHz |
|
|
|
|
|
#8 | |||||||||||||||||||
|
Registered User
Join Date: May 2007
Posts: 1
|
Hello!
I am rather new to AppleScript, but I'm trying to write simple script (using tips found here and elsewhere). The purpose of this script is to simply be executed as a folder action whenever a files are moved into this folder, then to see if among the files are .ps and if so, to move .ps files into IN folder, and all other files into Trash folder. So far I managed to write script which moves files, but it doesn't seem to care about extensions. I tried to use part of a picture processing script which had it coded and processed only graphical files, but it doesn’t seem to work. There seem to be a problem with getting file extension. If I add line so that it copy only .ps files, then it does not do anything. without that it copies, of course, all files. here is at it looks today:
I know its little messy, but its my first script in AppleScipt - and I just have no idea how to make it copy only .ps files. any ideas? ![]() thanks in advance |
|||||||||||||||||||
|
|
|
|
|
#9 |
|
League Commissioner
Join Date: Jan 2005
Posts: 8,475
|
Try this:
Code:
property dest_folder : "MacintoshHD:Users:**Your user name here**:Desktop:PS File In" --^ Be sure to set the correct path for your folder ^ on adding folder items to this_folder after receiving added_items tell application "Finder" repeat with aItem in added_items if (name of aItem) ends with ".ps" then move aItem to dest_folder else move aItem to trash end if end repeat end tell end adding folder items to |
|
|
|
|
|
#10 |
|
Hall of Famer
Join Date: Apr 2007
Posts: 4,262
|
alternate code...
Code:
property dest_folder : "Macintosh HD:PS File:IN:"
on adding folder items to this_folder after receiving added_items
set good_list to {}
set trash_list to {}
repeat with this_file in added_items
if (name extension of (info for this_file)) is "ps" then
set end of good_list to this_file
else
set end of trash_list to this_file
end if
end repeat
tell application "Finder"
move good_list to dest_folder
delete trash_list
end tell
end adding folder items to
|
|
|
|
|
|
#11 |
|
League Commissioner
Join Date: Jan 2005
Posts: 8,475
|
And I was worried that I was doing too much of it for him/her! I like bulk move and the name extension idea, but I tried mine with a file with a hidden extension and it worked. I'm wondering if it shouldn't have? I also don't like to delete files in a script, at least until I'm sure that everything is working perfectly. That's why I had mine only move them to the trash. |
|
|
|
|
|
#12 | ||||||||||||||||||||||||||||||||||||||||||
|
Hall of Famer
Join Date: Apr 2007
Posts: 4,262
|
sorry, still sleepy this morning. I'll try to be less helpful in the future. ![]()
delete in a Finder tell just moves things into the trashcan. it's not like unix rm which actually deletes stuff. as far as whether the hidden file thing should or shouldn't have worked... <shrug>. the finder in os X is a bit redundant (for backward compatability, I guess). I just assumed (without checking) that the finder would give the display name rather than the full name when you ask it for a name. my bad. |
||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#13 |
|
Guest
Posts: n/a
|
awesome... but...
Hi guys;
I've searched forever to find this service. Thank you for posting it. It'll come in super handy. But... I must be doing something wrong because it doesnt show up as a service when I select files etc... and right click to see my list of services. I see some services and when I go into the users library services etc... folder, I can see the workflow that I saved as a service. What am I doing wrong? (besides standing on the shoulders of giants, hehe) Drew |
|
|
|
#14 |
|
Guest
Posts: n/a
|
Hi guys;
I've tried to do this and when I click on it in automator it works. But when I save it as a service it doesnt show up in my "right click" options as a service, even though it is in the services folder. I'm not sure what I've done wrong. Drew |
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|