Extracting File Extensions from AppleScript Paths - directory

I'm writing an Apple Script that ultimately will mark commercials for all iTunes exports from EyeTV. But I'm running into a simple problem with AppleScript paths, which the EyeTV app returns as recording locations. Here's the context:
set recordingID to 370404006
set myid to recordingID as integer
tell application "EyeTV"
set eyetvr_file to get the location of recording id myid as alias
end tell
return eyetvr_file
alias "Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr"
Now I need to extract the containing path and the file prefix 000000001613eaa6 (using this question) so I can look for the corresponding commercial markings in the file 000000001613eaa6.edl. Here's the hiccup:
tell application "Finder"
set eyetv_path to container of eyetvr_file
set root_name to name of eyetvr_file
set fext to name extension of eyetvr_file
end tell
The results are,
eyetv_path: document file "South Park.eyetv" of folder "EyeTV Archive" of folder "Documents" of disk "Macintosh HD2" of application "Finder"
root_name: "000000001613eaa6.eyetvr"
fext: ""
fext should be ".eyetvr", not the empty string. How do I correctly extract ".eyetvr" from either eyetvr_file or root_name? I've tried a bunch of hack like
set fext to name extension of (eyetvr_file as document)
But this gives error like,
error "Can’t make alias \"Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr\" into type document." number -1700 from alias "Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr" to document

Finder doesn't recognize all filename extensions. You could just use text item delimiters to remove the last part though.
do shell script "touch /tmp/some.file.eyetvr"
set text item delimiters to "."
tell application "Finder"
set n to name of file (POSIX file "/tmp/some.file.eyetvr")
if n contains "." then set n to (text items 1 thru -2 of n) as text
n -- some.file
end tell

Mini-comments apparently do not allow posting code, so here are my edits to Lauri Ranta's nice solution:
do shell script "touch /tmp/some.file.eyetvr"
set delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
tell application "Finder"
set n to name of file (POSIX file "/tmp/some.file.eyetvr")
if n contains "." then set n to (text items 1 thru -2 of n) as text
n -- some.file
end tell
set AppleScript's text item delimiters to delims
I'll post a link to the EyeTV-commercial-marking-script-for-iOS when I've finished that project.

Related

Apple Script: when moving a file into a folder, is it possible to move duplicates inside that folder?

I have an apple script which moves all files within a folder into a new folder according to the first 6 characters of the filenames.
If the folder already exists, the files get moved into that existing folder instead.
If the file already exists in the new/existing folder, the file to be moved into the folder gets a "copy" extension to the filename – which is not in the script, it's a system function ;-)
My question:
Is it possible to move all existing (duplicate) files into a subfolder (named "_alt" for example), before the other files get moved within the folder (to prevent the "copy"-extension)? I would like to keep both files, the original files and the newly moved files.
Thanks.
This is my existing script:
set mgFilesFolder to (choose folder with prompt "Choose folder…")
(* get the files of the mgFilesFolder folder *)
tell application "Finder" to set fileList to files of mgFilesFolder
(* iterate over every file *)
repeat with i from 1 to number of items in fileList
set this_item to item i of fileList
set this_file_Name to name of this_item as string
set thisFileCode to characters 1 thru 6 of this_file_Name as text
log thisFileCode
tell application "Finder"
try
set nf to make new folder at mgFilesFolder with properties {name:thisFileCode}
end try
end tell
end repeat
set sourceFolder to mgFilesFolder
set destinationFolder to mgFilesFolder
tell application "Finder"
set the_files to (get files of mgFilesFolder)
repeat with this_file in the_files
set the_name to name of this_file
set the_short_name to items 1 thru 6 of the_name as string
if exists folder the_short_name of mgFilesFolder then move this_file to folder the_short_name of mgFilesFolder
end repeat
end tell

How to rename files within selected folder using directory folder and selected folder using Applescript

I'm wanting to run a script that renames the files within a selected folder by taking the name of the directory folder of the selected folder and the 2nd word from the selected folder and adding a sequencing number to the end of the file's name.
Here is what I have so far but all that is returning is {}. Please help me to fix this.
property file_count : -1
set prefix to pad(file_count as string)
tell application "Finder"
set theSel to item 1 of (get selection)
set parDir to name of container of theSel
set theName to name of theSel
set issNum to word 2 of theName
end tell
set file_count to file_count + 1
on pad(s)
repeat while length of s < 4
set s to ("0" & s)
end repeat
end pad
if file_count < 10 then
set file_count to ("00" & file_count)
else if file_count < 100 then
set file_count to ("0" & file_count)
end if
tell application "Finder"
set theName to parDir & " " & issNum & "-" & file_count
set theSubs to every file in theSel
repeat with I from 1 to count of theSubs
set name of every file of item I of theSubs to theName & ".jpg"
end repeat
end tell
theName result is "(the directory file name) 001-000"
Thank you in advance
l0g0p7
Edit:
as requested some examples of before and after for the file naming
Before: 08-GenerationX-1.jpg
After: Generation X -001-000.jpg
(the result comes from the parent directory of the selected folder being "Generation X" the second word in the selected folder is "-001" and then the numbering sequence suffix starting at 000 plus the file extention)
Note of caution: Please test this script on a copy of your files, not on the originals. Renaming files in bulk is a PITA to undo if you make a mistake; it's much easier to toss the mistake out and make a new copy from the original set.
This script should do what you want (I think). It takes the second word of the name of the selected folder, the name of its parent folder, and combines them with an index to rename the files in the folder. In line 5 it makes sure that the files are sorted by name before renaming them, to preserve file order. Let me know how it works.
set file_index to 0
tell application "Finder"
set selected_folder to item 1 of (get selection)
set selected_folder_path to POSIX path of (selected_folder as alias)
set files_to_rename to (sort files of selected_folder by name) as alias list
end tell
tell application "System Events"
set partition_directory to name of container of folder selected_folder_path
set section_name to name of folder selected_folder_path
set iss_number to word 2 of section_name
repeat with a_file in files_to_rename
set new_file_name to partition_directory & " " & iss_number & "-" & my zeroPad(file_index) & ".jpg"
set name of a_file to new_file_name
set file_index to file_index + 1
end repeat
end tell
on zeroPad(a_num)
return text -4 through -1 of ("0000" & a_num as string)
end zeroPad
P.s. I've redone your zero-padding routine with something a bit more efficient. I've also switched things over to System Events.app where possible. The Finder has a tendency to gum things up.

applescript - quicktime 7 audio (wav) export

I'm working on a script to help my workflow. My work involves sound design, and I often have to take videos and extract the audio. Regardless of the source/compression, I like it in .wav format - best quality, accepted by all audio editing software, and least overhead for playback in a live environment.
Currently, I use Quicktime Pro 7's Export feature for this task - the current Quicktime X doesn't export to .wav. Built into the OS, so instead of using a separate tool, I'm using QT.
I am using Automator to write a service - select the file, open it in QT, export as wav and save it in the same location as the original, then quit. Here is what I have so far, and I keep getting an error. "The action “Run AppleScript” encountered an error." It compiles properly, but nothing comes out. Late last night I got it to spit out .mov files for some reason (despite trying to tell it wave) and now I can't even get back there.
Any help is appreciated. As you can see from the commented parts, I was trying to specify anywhere, since trying to make it the same location as the original was escaping me. Currently I'm just having it prompt me on where to save, so I can just tackle one problem at a time. Cheers!
tell application "QuickTime Player 7"
--set saveFile to POSIX path of (((path to desktop) as Wave) & "test.wav")
set outfile to choose file name with prompt "Save altered file here:"
set error_states to {load state unknown, load error}
set successful_states to {loaded, complete}
repeat until load state of first document is in (error_states & successful_states)
delay 0.1
end repeat
tell document to save in outfile
if (load state of first document is in successful_states) then
if (can export first document as wave) then
export first document to outfile as wave
else
error "Cannot export " & (source_file as string) & " in .wav (Wave) format."
end if
else
error "File is not in a successful load state: " & (load state of first document as string)
end if
end tell
The file output should have '.wav" extension. To do so, I changed a bit your script and I remove the "tell document to save in outfile".
if you really want to delete your initial file after export, you need to add that at end of the script (I prefer to check the export is successful before deletion !).
Also, you must change the first line to replace the choose file (for my tests) to the read input variable from Automator service script :
set InFile to choose file "select video file for test only" -- to be replace by the input of your Automator Service
-- conversion of selected file with proper folder and new '.wav" extension
tell application "Finder"
set FFolder to (container of InFile) as string
set FName to name of InFile
set FExt to name extension of InFile
end tell
set Pos to offset of FExt in FName
if Pos > 0 then --change extension from current to 'wav'
set OutFile to FFolder & (text 1 thru (Pos - 1) of FName) & "wav"
else
set OutFile to FFolder & FName & ".wav"
end if
tell application "QuickTime Player 7"
activate
open InFile
set error_states to {load state unknown, load error}
set successful_states to {loaded, complete}
repeat until load state of first document is in (error_states & successful_states)
delay 0.1
end repeat
if (load state of first document is in successful_states) then
if (can export first document as wave) then
export first document to OutFile as wave
else
error "Cannot export " & (source_file as string) & " in .wav (Wave) format."
end if
else
error "File is not in a successful load state: " & (load state of first document as string)
end if
end tell

PROGRESS - Validating a user-input file output path

I've written some PROGRESS code that outputs some data to a user defined file. The data itself isn't important, the output process works fine. It's basically
DEFINE VARIABLE filePath.
UPDATE filePath /*User types in something like C:\UserAccount\New.txt */
OUTPUT TO (VALUE) filePath.
Which works fine, a txt file is created in the input directory. My question is:
Does progress have any functionality that would allow me to check if an input
file path is valid? (Specifically, if the user has input a valid directory, and if they have permission to create a file in the directory they've chosen)
Any input or feedback would be appreciated.
FILE-INFO
Using the system handle FILE-INFO gives you a lot of information. It also works on directories.
FILE-INFO:FILE-NAME = "c:\temp\test.p".
DISPLAY
FILE-INFO:FILE-NAME
FILE-INFO:FILE-CREATE-DATE
FILE-INFO:FILE-MOD-DATE
FILE-INFO:FILE-INFO
FILE-INFO:FILE-MOD-TIME
FILE-INFO:FILE-SIZE
FILE-NAME:FILE-TYPE
FILE-INFO:FULL-PATHNAME
WITH FRAME f1 1 COLUMN SIDE-LABELS.
A simple check for existing directory with write rights could be something like:
FUNCTION dirOK RETURNS LOGICAL (INPUT pcDir AS CHARACTER):
FILE-INFO:FILE-NAME = pcDir.
IF INDEX(FILE-INFO:FILE-TYPE, "D") > 0
AND INDEX(FILE-INFO:FILE-TYPE, "W") > 0 THEN
RETURN TRUE.
ELSE
RETURN FALSE.
END FUNCTION.
FILE-NAME:FILE-TYPE will start with a D for directories and a F for plain files. It also includes information about reading and writing rights. Check the help for more info. If the file doesn't exist basically all attributes except FILE-NAME will be empty or unknown (?).
Edit: it seems that FILE-TYPE returns W in some cases even if there's no actual writing rights in that directory so I you might need to handle this through error processing instead
ERROR PROCESSING
OUTPUT TO VALUE("f:\personal\test.txt").
PUT UNFORMATTED "Test" SKIP.
OUTPUT CLOSE.
CATCH eAnyError AS Progress.Lang.ERROR:
/* Here you could check for specifically error no 98 indicating a problem opening the file */
MESSAGE
"Error message and number retrieved from error object..."
eAnyError:GetMessage(1)
eAnyError:GetMessageNum(1) VIEW-AS ALERT-BOX BUTTONS OK.
END CATCH.
FINALLY:
END FINALLY.
SEARCH
When checking for a single file the SEARCH command will work. If the file exists it returns the complete path. It does however not work on directory, only files. If you SEARCH without complete path e g SEARCH("test.p") the command will search through the directories set in the PROPATH environment variable and return the first matching entry with complete path. If there's no match it will return unknown value (?).
Syntax:
IF SEARCH("c:\temp\test.p") = ? THEN
MESSAGE "No such file" VIEW-AS ALERT-BOX ERROR.
ELSE
MESSAGE "OK" VIEW-AS ALERT-BOX INFORMATION.
SYSTEM-DIALOG GET-FILE character-field has an option MUST-EXIST if you want to use a dailogue to get filename/dir from user. Example from manual
DEFINE VARIABLE procname AS CHARACTER NO-UNDO.
DEFINE VARIABLE OKpressed AS LOGICAL INITIAL TRUE.
Main:
REPEAT:
SYSTEM-DIALOG GET-FILE procname
TITLE "Choose Procedure to Run ..."
FILTERS "Source Files (*.p)" "*.p",
"R-code Files (*.r)" "*.r"
MUST-EXIST
USE-FILENAME
UPDATE OKpressed.
IF OKpressed = TRUE THEN
RUN VALUE(procname).
ELSE
LEAVE Main.
END.

Add file at root of zip file using DotNetZip with classic ASP

I have DotNetZip installed and running fine on a Windows 2008 server.
Using a classic ASP page, I want to bundle a bunch of comma-delimited files to a user and send it over in a zip file.
The following code works fine but it stores all the path information so the files inside the zip file are located in some ridiculous directory like C:\Inetpub\wwwroot\appname\_temp\
I'm using the following code:
Set objZip = CreateObject("Ionic.Zip.ZipFile")
sFileArray = Split(sFileArray, "|")
For iCount = 0 To UBound(sFileArray)
If sFileArray(iCount) <> "" Then
objZip.AddFile sFileArray(iCount)
End If
Next
objZip.Name = sFilePath & "test.zip"
objZip.Save()
objZip.Dispose()
Set objZip = Nothing
I see that the AddFile method allows you to specify where you want the added file to reside in the zip file if you add a second parameter. According to the documentation objZip.AddFile sFileArray(iCount), "" should put the file in the root of the zip file.
However, when I add that parameter, I get the following error:
Wrong number of arguments or invalid property assignment: 'objZip.AddFile'
Anyone have any idea what I'm doing wrong?
Thanks.
I think you are misinterperting the documentation. If the second parameter is null then the directory path of the file being added is used. If the second parameter is an empty string "" then the file is added to the root level in the zip. A quick look into the Ioniz.zip.dll shows that the single parameter override of AddFile method simply calls the the double parameter override with the second parameter set to null.
Hence your add file should look like:
objZip.AddFile sFileArray(iCount), ""
to get the result you are after.

Resources