Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 04-16-2007, 07:38 AM   #1
ldrury
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
ldrury is offline   Reply With Quote
Old 04-16-2007, 10:16 AM   #2
capitalj
All Star
 
Join Date: Dec 2005
Location: Plymouth, Massachusetts
Posts: 630
The problem is in this line:
Code:
move x to thefoldername
where "thefoldername" is the variable for the name of the created folder. You are actually trying to move the file(s) to the name of the new folder, not the folder itself. You need a reference to the created folder. Try this instead:
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
capitalj is online now   Reply With Quote
Old 04-16-2007, 04:15 PM   #3
ldrury
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
ldrury is offline   Reply With Quote
Old 04-17-2007, 11:54 AM   #4
capitalj
All Star
 
Join Date: Dec 2005
Location: Plymouth, Massachusetts
Posts: 630
Quote:
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???

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
the result of which was (copied from the event log)
Quote:
get selection
{document file "Test.rtf" of folder "Test Folder" of folder "J" of folder "Users" of startup disk}

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)
Quote:
make new folder at alias "Macintosh HD:Users:J:Test Folder:" with properties {name:"New Test Folder"}

and the result - a Finder reference - is shown next
Quote:
folder "New Test Folder" of folder "Test Folder" of folder "J" of folder "Users" of startup disk

I hope that was understandable.
capitalj is online now   Reply With Quote
Old 04-17-2007, 05:30 PM   #5
ldrury
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
ldrury is offline   Reply With Quote
Old 04-17-2007, 06:26 PM   #6
capitalj
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
capitalj is online now   Reply With Quote
Old 04-17-2007, 06:40 PM   #7
ldrury
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
ldrury is offline   Reply With Quote
Old 05-06-2007, 07:28 AM   #8
ER SYS
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:

Quote:
property source_folder : "Macintosh HD:PS File:SCR:"
property dest_folder : "Macintosh HD:PS File:IN:"
property TypeList : {"PS"}
property trash_folder : "Macintosh HD:PS File:trash:"

on run
delay 0
set files_to_move to {}
set FileList to {}
set ItemList to list folder source_folder without invisibles
set folder_contents to list folder (source_folder as alias) without invisibles
repeat with aItem in folder_contents
set current_item to source_folder & aItem as string
set TheInfo to info for (file (source_folder))
if folder of TheInfo is false then
if file type of TheInfo is in TypeList then
set files_to_move to files_to_move & current_item
end if
end if
end repeat
if (count of files_to_move) > 0 then
set copied_files to my copy_the_files(files_to_move, dest_folder)
if ((count of copied_files) = (count of files_to_move)) then
set deleted_files to my delete_the_files(files_to_move)
end if

else
set copied_files to my copy_the_files(FileList, trash_folder)
if ((count of copied_files) = (count of FileList)) then
set deleted_files to my delete_the_files(FileList)
end if

end if
end run
on copy_the_files(FileList, destination)
set return_list to {}
repeat with aItem in FileList
tell application "Finder"
activate
set return_list to return_list & ((duplicate (aItem as alias) to (destination as alias) with replacing) as string)
end tell
end repeat
return return_list
end copy_the_files

on delete_the_files(FileList)
set return_list to {}
repeat with aItem in FileList
tell application "Finder"
activate
delete (aItem as alias)
end tell
end repeat
return return_list
end delete_the_files

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
ER SYS is offline   Reply With Quote
Old 05-06-2007, 07:58 AM   #9
cwtnospam
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
cwtnospam is offline   Reply With Quote
Old 05-06-2007, 09:24 AM   #10
tw
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
this will pick up files that have their extensions hidden, and it uses the Finder's 'bulk move' capability, which will save a lot of time if you drop a big chunk of files in at the same time.
tw is offline   Reply With Quote
Old 05-06-2007, 09:36 AM   #11
cwtnospam
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.
cwtnospam is offline   Reply With Quote
Old 05-06-2007, 10:21 AM   #12
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
Quote:
Originally Posted by cwtnospam
And I was worried that I was doing too much of it for him/her!

sorry, still sleepy this morning. I'll try to be less helpful in the future.

Quote:
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.

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.
tw is offline   Reply With Quote
Old 06-12-2012, 10:17 PM   #13
Druv3us
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
  Reply With Quote
Old 06-12-2012, 10:24 PM   #14
Druv3us
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
  Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 04:58 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.