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")
Related
I have a script that starts like this:
tell application "Finder"
set theFolder to (choose folder)
set theFile to POSIX path of ((theFolder as string) & "words.txt")
set fileHandle to open for access theFile
set nameArray to paragraphs of (read fileHandle for (get eof fileHandle) as «class utf8»)
close access fileHandle
end tell
File words.txt is there and contains one word per line.
theFile is a valid path to words.txt, something like /Users/myself/Desktop/folder/words.txt.
nameArray comes empty.
Why?
If instead of letting the user choose the folder, I hardcode the path, like
set theFile to "/Users/myself/Desktop/folder/words.txt"
everything works fine.
First of all, you do not need Finder as the necessary commands are just basic AppleScript commands and or are all a part of Standard Additions.
The following three lines, by themselves, will do what you are trying to do:
set theFolder to (choose folder)
set theFile to POSIX path of ((theFolder as string) & "words.txt")
set nameArray to paragraphs of (read theFile as «class utf8»)
Something to keep in mind, if the last line in the file ends with a line feed then the last item in the list will be "" and you can either account for this in your code as you use each item of the list or add the following example to remove it if it exists:
if last item of nameArray is equal to "" then ¬
set nameArray to items 1 thru -2 of nameArray
I am trying to do below in AppleScript.
Concatenate/Merge all *.xxx files found in a particular folder into one new file
Each file contains a header. Strip header from all but 1st file before merging.
Add a footer text to the merged file.
This sounds relatively simple in other languages but I am a beginner to applescript. Any help to find a direction would be appreciated.
TIA
AnuRV
Try this, you are prompted to choose a base folder and a destination file name.
Important: Use a destination location outside the base folder to avoid the file to be included in the merging process.
I assume that your tsv file type is a typo and you mean csv.
If not, change all occurrences of csv in the script.
The text delimiter is linefeed (0A), if you need return (0D) change the occurrence of linefeed to return.
set baseFolder to choose folder
set destinationFile to choose file name with prompt "Choose destination file name" default name "merged.csv"
tell application "Finder" to set tsvFiles to (files of baseFolder whose name extension is "csv") as alias list
set text item delimiters to linefeed
try
set fileDescriptor to open for access destinationFile with write permission
repeat with i from 1 to (count tsvFiles)
set theFile to item i of tsvFiles
set theText to paragraphs of (read theFile as «class utf8»)
if i = 1 then
write (theText as text) to fileDescriptor as «class utf8»
else
write ((items 2 thru -1 of theText) as text) to fileDescriptor as «class utf8»
end if
end repeat
close access fileDescriptor
on error
try
close destinationFile
end try
end try
set text item delimiters to {""}
I have an AppleScript that runs as part of a Hazel routine monitoring a folder. When the script runs, it picks apart the file targeted by the Hazel routine and then attaches the file to an email and addresses the email with information from the name of the file. Unfortunately, it seems there is an error somewhere in the script, but I cannot seem to locate it.
The only semi-useful information from Console is in the title (i.e., "Can't get last text item of alias"). Here is the script:
on hazelProcessFile(theFile)
set theAttachment1 to (POSIX path of theFile)
set FileName to theFile
--remove trailing slash
set SansSlash to quoted form of text 1 through -2 of FileName
set FileName to SansSlash as text
-- remove path from FileName
set AppleScript's text item delimiters to ":"
set SansPath to last text item of FileName
set FileName to SansPath as text
-- remove extension
set AppleScript's text item delimiters to "."
set SansExtension to every text item of FileName
set last text item of SansExtension to ""
set FileName to SansExtension as text
-- parse using —
set AppleScript's text item delimiters to "—"
set clientName to first text item of FileName
set clientEmail to last text item of FileName
tell application "Airmail 2"
activate
set theMessage to make new outgoing message with properties {subject:"New Invoice from ", content:"Please find attached, infra, the current month's invoice. If you have any questions, please feel free to respond to this email. One-time payments may be made using the following secure form on our website: https://. Thank you for your continued business."}
tell theMessage
set sender to "billing#example.com"
make new to recipient at end of to recipients with properties {name:clientName, address:clientEmail}
make new mail attachment with properties {filename:theAttachment1}
compose
end tell
end tell
end hazelProcessFile
The code is commented, so it should be obvious what each section is supposed to do. I would imagine the issue is in the "remove path from FileName" section, as that is the section that has been giving me the most trouble.
theFile is obviously an alias specifier.
text item and text thru – as the name implies – expects plain text
You have to coerce the alias first to text before dealing with text and remove quoted form of, that's only needed in conjunction with do shell script.
--remove trailing slash
set FileName to theFile as text
set SansSlash to text 1 through -2 of FileName
set FileName to SansSlash
but there is no trailing slash in a HFS path
To strip the file name without extension from an alias this is much easier
tell application "System Events" to set {name:Nm, name extension:Ex} to theFile
set baseName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
Edit:
Try this optimized code, the Airmail 2 part is skipped and I don't know if the EM Dash character is treated correctly in case you'll copy and paste the code.
on hazelProcessFile(theFile)
set theAttachment1 to (POSIX path of theFile)
tell application "System Events" to set {name:Nm, name extension:Ex} to theFile
set FileName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
-- parse using —
set {TID, text item delimiters} to {text item delimiters, "—"}
set clientName to text item -2 of FileName
set clientEmail to text item -1 of FileName
set text item delimiters to TID
-- tell application "Airmail 2"
-- ...
end hazelProcessFile
I've got a choose file in my AppleScript. When I run the script and choose a file, the output is always the full file path with the file extension on the end. For example:
Macintosh HD:Developer:About Xcode.pdf
is what I don't want. I only want:
About Xcode
The below answer by Kassym Dorsel doesn't work when there is more than one . in it.
The below answer by Lri doesn't work with set x to choose file:
error "Can’t make quoted form of alias \"Macintosh HD:Applications:Firefox.app:\" into type Unicode text." number -1700 from quoted form of alias "Macintosh HD:Applications:Firefox.app:" to Unicode text
You can use the Finder to manipulate the names of Finder items:
choose file with prompt "Pick one"
set filepath to result
tell application "Finder" to set {dispName, nameExt, isHidden} to ¬
the {displayed name, name extension, extension hidden} of the filepath
if isHidden or nameExt is equal to "" then
dispName
else
(characters 1 through (-2 - (count of nameExt)) of dispName) as text
end if
set baseName to result
This will work :
set a to "Macintosh HD:Developer:About.Xcode.pdf"
set text item delimiters to ":"
set temp to last text item of a
set text item delimiters to "."
set temp to text items 1 thru -2 of temp as text
Gives => About.Xcode
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.