Jump to content

Renaming and moving a folder after a FM import


This topic is 7772 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I'd like to write an Applescript to move a folder after Fm runs a certain script.

Basically, every morning we import our orders that have been batched from our online estore database into our orders, customers and lineitems databases. What I would want the applescript to do is then copy the folder to our server and name it the current date.

Is this something relatively simple? I have no Applescripting experience.

Link to comment
Share on other sites

The syntax should be something like...

set dateString to date string of (current date)

Tell app "Finder"

move folder oldFolderPath to folder FolderDestinationPath

set name of the result to dateString

end tell

If the newFolderDestination is on the same volume, the Finder will move it. But if is on a different volume, the the Finder will duplicate the folder to the new location and the old folder will remain. The Finder works with object reference which are different then the aliases used within applescripts. Stick to using the path as a text string and you will avoid some pitfalls.

Try this script to get started...

set dateString to date string of (current date)

set oldFolderAlias to choose folder with prompt "Select folder to move"

set FolderDestinationAlias to choose folder with prompt "Select Destination for folder"

set oldFolderPath to oldFolderAlias as string

set FolderDestinationPath to FolderDestinationAlias as string

tell application "Finder"

move folder oldFolderPath to folder FolderDestinationPath

set name of the result to dateString

end tell

[Just using the AppleScript aliases works, but it has been my experience that it does not work as reliably as using the file paths. I am wondering if my practice is this is a hold-over from buggy earlier AS versions. Anybody know?]]

Link to comment
Share on other sites

Ok, this looks good. I'm starting to see how it works.

How would I modify that script so that it copies the original folder, renames the copy and then saves it to the destination folder?

So, it would be the same thing except, it leaves the original folder in place.

Even better, would be if it went one more step and deleted the contents of the original folder.

Link to comment
Share on other sites

Just change the "move folder" to "duplicate folder"

you can trash items by using the syntax...

move whatever to the trash

Then the item is in the trash, but you can empty the trash using AS. Trashing everthing in the folder would be more difficult than trashing the whole folder and then recreating it from scratch ...

make new folder at folder theDestinationFolder with properties {name: "FolderName"}

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 months later...
  • 2 weeks later...

Sorry it took me so long to get back to this thread. In regard to OSX compatiblity, the script works fine for me, without changes, under OSX. It does get an error if you try to run the script twice in one day, as it will not rename the folder with the name of a folder that already at the same location.

Regarding your previous post: following is a subroutine that will format dates into a short string. It is a modified version of a script from AppleScript this week and more info can be found at http://www.macadillo.com/ATW/shortdate.html

Just change the first line of my script to

set dateString to FormatDate(current date)

and then add the subroutine below to the end of your script

on FormatDate(theDate)

-- format: dd/mm/yyyy or mm/dd/yyyy depending on settings in Date & Time panel

-- get the day number with leading zero

set thisDay to text -2 thru -1 of ("0" & day of theDate)

-- get the month number with leading zero

copy theDate to b

set month of b to January

set thisMonth to text -2 thru -1 of ("0" & (1 + (theDate - b + 1314864) div 2629728))

-- define the order of the month & day

try

-- try to coerce the word to an integer

word 2 of (theDate as string) as integer -- will error on US date format

set theFirstPart to thisDay & "/" & thisMonth

on error

set theFirstPart to thisMonth & "/" & thisDay

end try

-- make the string

set theShortDate to theFirstPart & "/" & (year of theDate) as text

return theShortDate

end FormatDate

Link to comment
Share on other sites

I am guessing it is because there is already a folder with that name at the same location. OS X is more sensitive to duplicate file names than 9.x. If that is the source of the error, you can trap the error and apend the name with a number. Replace the line

set name of the result to dateString

with the following...

set theNewFolder to the result

try

set name of theNewFolder to dateString

on error

repeat with i from 1 to 9

try

set name of the theNewFolder to (dateString & i) as string

exit repeat

end try

end repeat

end try

This is not the most sophisticated error trapping, but if it is an expected error (duplicated folder names) it is a good way around the problem.

Link to comment
Share on other sites

Actually, the problem occurs later after trashing the source folder and trying to create it again. That's when the error occurs. Maybe this is because the original folder "Daily Import" is in the trash?

So, your script helps if the source folder already exists (the date one), but not when I try to trash the orignal source folder and try to recreate it.

I'm probably not making any sense. Sorry, I'm still just figuring out Applescript.

Link to comment
Share on other sites

I don't know what the problem is. I just tried the following and it worked just fine under OSX

set theDestinationFolder to choose folder

repeat

tell application "Finder"

make new folder at folder theDestinationFolder with properties {name:"FolderName"}

move result to the trash

display dialog "Success"

end tell

end repeat

Please post your script and we'll see what we can do.

Link to comment
Share on other sites

This is where I am now:

set dateString to FormatDate(current date)

set oldFolderAlias to "Daily Import"

set FolderDestinationAlias to "Daily Import"

set oldFolderPath to oldFolderAlias as string

set FolderDestinationPath to FolderDestinationAlias as string

tell application "Finder"

duplicate folder oldFolderPath to folder "2003 Batches Archive" of folder "Batches" of folder "Database related" of disk "File Server"

set name of the result to dateString

select folder "Daily Import"

delete selection

make new folder at desktop

select folder "untitled folder"

set name of selection to "Daily Import"

end tell

on FormatDate(theDate)

-- format: dd/mm/yyyy or mm/dd/yyyy depending on settings in Date & Time panel

-- get the day number with leading zero

set thisDay to text -2 thru -1 of ("0" & day of theDate)

-- get the month number with leading zero

copy theDate to b

set month of b to January

set thisMonth to text -2 thru -1 of ("0" & (1 + (theDate - b + 1314864) div 2629728))

-- define the order of the month & day

try

-- try to coerce the word to an integer

word 2 of (theDate as string) as integer -- will error on US date format

set theFirstPart to thisDay & "/" & thisMonth

on error

set theFirstPart to thisMonth & "/" & thisDay

end try

-- make the string

set theShortDate to theFirstPart & "/" & (year of theDate) as text

return theShortDate

end FormatDate

Thanks.

M

Link to comment
Share on other sites

I haven't analyzed your entire logic, and I'm not the quickest with AppleScript, but one thing jumped out immediately.

When you address a folder or file, you have to use the entire path, either in Finder format (folder "something" of folder "enclosing" of disk "hardy disk") or as text file path (alias "Macintosh Hard Drive:Folder1:Folder Inside:File Name") or "Macintosh Hard Drive:Folder1:Folder Inside:" for a folder.

You can't just say select folder "Folder name." It doesn't know where it is.

I don't think you have to select it first anyway. You can just delete it in one step, with the full file (folder) path (a folder is just a special kind of file, AFAIK).

Link to comment
Share on other sites

Fenton is right and wrong. You can say select folder "Folder Name" as long as the folder is on you desktop and you use the construct in a Tell app "Finder" block. (I was not aware that works, but it does, at least with system 9.x.) I haven't checked yet, but with the changes in the way the desktop is handled this is probably the source of the problem in OS X. Spell out the entire path. Alternatively, you can set a Property and the script will remember a given location until the next time the script is compiled...

property oldFolderAlias : ""

tell application "Finder"

if not (exists folder oldFolderAlias) then

set oldFolderAlias to choose folder with prompt "Select a folder for import"

end if

end tell

[This will only work with freestanding AppleScripts. Properties are not saved in AppleScripts run within FM ScriptMaker scripts.]

Your script needs more work. What you've got shows the pitfalls and promise of using script recording to create a script. Keep on trying, learning and working on it. Programming is not for the fainthearted. I'll try to get some time this weekend to help you out. Lots of luck.

Link to comment
Share on other sites

This topic is 7772 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.