capturing edited file name from duplicated file - applescript

I copy a a file to a new folder then rename it. Next I want to open it:-
set myNewFile to duplicate myFile to myNewLocation
set the name of myNewFile to myNewFileName
open myNewFile
The first 2 lines above work but the open command doesn't work because it can't find the file since I renamed it. I want to do something like:-
open myNewLocation & myNewFileName
myNewLocation is a valid path, myNewFileName is just a string, I have all the data I need but I can't work out how to construct a valid usable path from the two items.
Alternatively I could maybe change the name at the same time as I duplicate the file, before I save it to myNewFile. I've tried:-
set myNewFile to duplicate myFile to myNewLocation with name myNewFileName
and
set myNewFile to duplicate myFile to myNewLocation with properties {name:myNewFileName}
Neither works.

Try:
tell application "Finder" to set myFile to file "Mac OS X:Users:JD:Desktop:testStart.txt"
set myNewLocation to (path to desktop as text)
set myNewFileName to "testEnd.txt"
set myNewFile to duplicate myFile to myNewLocation
set the name of myNewFile to myNewFileName
tell application "Finder" to open file (myNewLocation & myNewFileName)

Related

I wanna to to create and open Text file via Applescript

For now I can generate the text file with applescript with this code
tell application "Finder"
try
set fileName to "untitled"
if length of fileName = 0 then
return 0
end if
set fileExt to ".txt"
set thisFolder to the target of the front window as alias
set newFile to fileName & fileExt
make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"}
on error errMsg
display dialog (errMsg)
end try
end tell
but I want to open this new file that just created by the script instantly
May anyone suggest that ?
To open the newly created text file immediately, you could change:
make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"}
To:
open (make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"})
This will immediately open the newly created text file in the application set as the default to handle this type of file. By default, if it hasn't been changed, it will be opened in TextEdit.

AppleScript: Garbage values written to file when using path to command

I am trying to write a simple applescript that organises the files in any selected folder. I want to make the script such that it runs at specific intervals and re-organizes the folder if something has changed. For this, I am trying to save the path of the user chosen folder to a file. Each time the script runs, it reads the folder path from this file.
here's a snippet from the code:
set home_path to get path to home folder
tell application "Finder"
set home_folder to folder (home_path as string)
if not (exists file "Clfd_config.cf1" in home_folder) then
set (folder_path) to choose folder with prompt "Choose the folder to organize"
set this_folder to folder (folder_path as string)
set path_file to open for access file (home_path & "Clfd_config.cf1" as text) with write permission
write folder_path to path_file
close access path_file
else
set path_file to open for access file (home_path & "Clfd_config.cf1" as string)
set folder_path to read path_file as string
set this_folder to folder (folder_path as string)
close access path_file
end if
end tell
However, when I open the file, it has garbled information, like so:
������Harshad��������������������œ‘xH+��� 7 Desktop����������������������������������������� ���������������� 7Éœ‘zç��������ˇˇˇˇ��I ���������� ������œ‘*∆������œ‘-5������D�e�s�k�t�o�p��� �H�a�r�s�h�a�d��Users/harshad/Desktop���/����ˇˇ������
When I try to read this file n the script, the script obviously fails.
I have tried telling the script to write the file as string, as text, but I keep getting the error that the folder_path variable cannot be converted to text or string.
What should I do so that the path is saved properly and the script can read it back from the saved file?
The main issue is that you're writing an alias file specifier to disk rather than a string path.
I added basic error handling while writing to disk and removed some redundant code
property configFileName : "Clfd_config.cf1"
tell application "Finder"
set configFile to (home as text) & configFileName
if not (exists file configFile) then
set folder_path to choose folder with prompt "Choose the folder to organize"
try
set fileReference to open for access file configFile with write permission
write (folder_path as text) to fileReference
close access fileReference
on error
try
close access file configFile
end try
end try
else
set folder_path to read file configFile
set this_folder to folder folder_path
end if
end tell
It seems you just want to save value of a folder path and be able to find it again during next run.
if only that, why are you using subroutine to write this path to a text file at specific place ? isn't it easier to you use the characteristics of "property" objects ?
"Property" variable can be changed and it keeps new value in the script itself, until next compilation.
Try this script bellow : the first run, it will ask you for folder. Select it. Any next run, it will just display the folder selected during first run !
property My_Folder : ""
if My_Folder is "" then -- first run, ask user to select folder
Set My_Folder to (choose folder with prompt "choose the folder to organise") as string
end if
display dialog "folder selected = " & My_Folder
it does the same thing than read/write from text file...

Error message on Applescript duplicate and rename

I use this applescript to Archive episodes to a new location using a smal reference-file "Archived.m4v" which is then renamed to the episodes name.
I keep getting an error message in OSX Yosemite, while under OSX Mavericks it worked perfectly.
error "The variable NewFile is not defined." number -2753 from "NewFile"
All questions related to a script like this have the same code, so I'm going nuts here...
set TheFile to alias "Video:Tools:Archived.m4v"
set Destination to alias "Video:Archives:WIP - TV Shows:"}
set Source to (choose folder with prompt "Pick the folder with the tv episodes...")
tell application "Finder"
set theList to every file of entire contents of Source
repeat with thisFile in theList
set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
set FileName to (text items 1 thru -2 of (get name of thisFile)) as text
set NewFile to duplicate TheFile to folder Destination with replacing
set NewFile's name to (FileName & ".m4v")
end repeat
end tell
The only obvious mistake I see is in your duplicate line. Destination is already an alias so it should not have the word "folder" in front of it. I can't test this right now but try removing "folder" and see if that helps.
The only other thing to try if that didn't fix it is to change your last line to...
set name of file ((destination as text) & "Archive.m4v") to (FileName & ".m4v")

Create new folder from files name and move files

(This is a new edit from a previous question of mine which achieved -3 votes. Hope this new one has a better qualification)
I need to create an Automator service to organize a high amount of files into folders. I work with illustrator and from each .ai file I create 3 more formats: [name.pdf], [name BAJA.jpg] and [name.jpg], thats 4 files in total
My problem is that during the week I repeat this process to more than 90 different .ai files. So 90 files * 4 is 360 independent files all into the some project folder.
I want to grab all 4 related files into one folder, and set the folder name as the same as the .ai file.
Since all the file names are identical (except one), I thought of telling the finder to grab all the files with the same name, copy the name, create a folder and put this files inside, but I have a file name variant [name LOW.jpg] Maybe I can tell the script to strip that work as an exception.
That way I will all 4 the files unified into one folder.
Thank you in advance
Update: This problem was originally posted back in 2013, now I have a solution. People help me assembled this script to fit my needs.
I added this as a service and assigned a keyboard shurtcut on MacOs.
This is the code:
on run {input, parameters} -- create folders from file names and move
set output to {} -- this will be a list of the moved files
repeat with anItem in the input -- step through each item in the input
set {theContainer, theName, theExtension} to (getTheNames from anItem)
try
# check for a suffix and strip it off for the folder name
if theName ends with " BAJA" then
set destination to (makeNewFolder for (text 1 thru -6 of theName) at theContainer)
else
set destination to (makeNewFolder for theName at theContainer)
end if
tell application "Finder"
move anItem to destination
set the end of the output to the result as alias -- success
end tell
on error errorMessage -- duplicate name, permissions, etc
log errorMessage
# handle errors if desired - just skip for now
end try
end repeat
return the output -- pass on the results to following actions
end run
to getTheNames from someItem -- get a container, name, and extension from a file item
tell application "System Events" to tell disk item (someItem as text)
set theContainer to the path of the container
set {theName, theExtension} to {name, name extension}
end tell
if theExtension is not "" then
set theName to text 1 thru -((count theExtension) + 2) of theName -- just the name part
set theExtension to "." & theExtension
end if
return {theContainer, theName, theExtension}
end getTheNames
to makeNewFolder for theChild at theParent -- make a new child folder at the parent location if it doesn't already exist
set theParent to theParent as text
if theParent begins with "/" then set theParent to theParent as POSIX file as text
try
return (theParent & theChild) as alias
on error errorMessage -- no folder
log errorMessage
tell application "Finder" to make new folder at theParent with properties {name:theChild}
return the result as alias
end try
end makeNewFolder
Hope this helps.
It's a pity you get downvoted as I, personally, enjoy answering these sorts of questions, as it helps me practise and improve my own skills.
Thanks for posting your solution. I think it's a great gesture and others will find it useful.
This script is a bit shorter than and uses "System Events" instead of "Finder", so will be quicker for large numbers of files:
set IllustratorOutputFolder to "/Users/CK/Desktop/example"
tell application "System Events" to ¬
set ai_files to every file in folder IllustratorOutputFolder ¬
whose name extension is "ai"
set Output to {}
repeat with ai_file in ai_files
set AppleScript's text item delimiters to "."
get name of ai_file
get text items of result
set basename to reverse of rest of reverse of result as text
tell application "System Events"
get (every file in folder IllustratorOutputFolder ¬
whose name begins with basename)
move result to (make new folder ¬
in folder IllustratorOutputFolder ¬
with properties {name:basename})
end tell
set end of Output to result
end repeat
return Output -- list of lists of moved files
Just an alternative way of doing things. Not that it's better or worse, but just a different solution.
You could also save this as script.sh (in TextEdit in plain text mode) and run it with bash script.sh in Terminal:
cd ~/Target\ Folder/
for f in *.ai *.pdf *.jpg; do
dir=${f%.*}
dir=${dir% LOW}
mkdir -p "$dir"
mv "$f" "$dir"
done

Applescript create and rename file from other files

In order to import .MOV files (h.264) to Final Cut Pro I need a correspoding .THM file with the same filename as the .MOV. Is it possible to do this with an AppleScript or Automator? Here is what I want to do:
Create a copy of a "TEMPLATE.THM" file that already exists on my HD
Rename the "TEMPLATE.THM" file using the .MOV filename
Do this to a folder of .MOV files to create a .THM file for every .MOV file both with the same filename.
G'day
This might not be the quickest way — but I see you're still waiting for an answer — so here's something to get you started. Select all your MOV files in the finder and run this in script editor.
set theTemplate to "Macintosh HD:Users:[user name]:[folder:location]:TEMPLATE.THM"
tell application "Finder"
set theFiles to selection
repeat with thisFile in theFiles
set thisName to name of thisFile
set theFolder to container of thisFile
set newFile to duplicate theTemplate to theFolder
set text item delimiters of AppleScript to "."
set thisName to text item 1 of thisName
set text item delimiters of AppleScript to ""
set newName to (thisName & ".THM")
set name of newFile to newName
end repeat
end tell
The easiest way to get the path to the template is to select it in the finder and run this :
tell application "Finder"
set theFile to selection as string
end tell
That will put the path in your results window — just copy it into the first line of the script above.
Hope that helps
m.

Resources