Applescript error while trying to move files with hazel - string

I have an applescript (below) which is used to clean the name of a tv show episode, and move the episode to its right folder based on that. I use hazel to activate the script with the right file. In applescript editor, the script seems fine, but Hazel gives me an error on line 12. Here's the script:
set theSample to theFile
set a to RemoveListFromString(theSample, {"720p", "HDTV", "x264", "IMMERSE", "-", "E01"})
set b to RemoveListFromString(a, {"..."})
set c to RemoveListFromString(b, {".."})
set d to replaceString(c, "S0", "Season ")
set e to replaceString(d, ".", " ")
if e contains "Season" then
move theFile to "/Volumes/LaCie/Midia/Series/" & e
end if
if e does not contain "Season" then
move theFile to "/Volumes/LaCie/Midia/Filmes"
end if
on RemoveFromString(theText, CharOrString)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local ASTID, theText, CharOrString, lst
set ASTID to AppleScript's text item delimiters
try
considering case
if theText does not contain CharOrString then ¬
return theText
set AppleScript's text item delimiters to CharOrString
set lst to theText's text items
end considering
set AppleScript's text item delimiters to ASTID
return lst as text
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't RemoveFromString: " & eMsg number eNum
end try
end RemoveFromString
on RemoveListFromString(theText, listOfCharsOrStrings)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local ASTID, theText, CharOrString, lst
try
script k
property l : listOfCharsOrStrings
end script
set len to count k's l
repeat with i from 1 to len
set cur_ to k's l's item i
set theText to my RemoveFromString(theText, cur_)
end repeat
return theText
on error eMsg number eNum
error "Can't RemoveListFromString: " & eMsg number eNum
end try
end RemoveListFromString
-- e.g. replaceString("Hello hello", "hello", "Bye") --> "Hello Bye"
on replaceString(theText, oldString, newString)
-- ljr (http://applescript.bratis-lover.net/library/string/)
local ASTID, theText, oldString, newString, lst
set ASTID to AppleScript's text item delimiters
try
considering case
set AppleScript's text item delimiters to oldString
set lst to every text item of theText
set AppleScript's text item delimiters to newString
set theText to lst as string
end considering
set AppleScript's text item delimiters to ASTID
return theText
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't replaceString: " & eMsg number eNum
end try
end replaceString
Any ideas?
EDIT:
So, now I have this:
on hazelProcessFile(theFile)
set text item delimiters to ":"
set filename to last text item of (theFile as text)
set text item delimiters to "."
if filename contains "." then
set base to text items 1 thru -2 of filename as text
set extension to "." & text item -1 of filename
else
set base to filename
set extension to ""
end if
set text item delimiters to {"720p", "HDTV", "x264", "IMMERSE", "-", "E01", "…", "E02", "EVOLVE"}
set ti to text items of base
set text item delimiters to ""
set newbase to ti as text
set newbase to Replace(newbase, "S0", "Season ")
set newbase to Replace(newbase, ".", " ")
set newbase to Replace(newbase, " ", "")
set newbase to Replace(newbase, " ", "")
set folderLocation to "/Volumes/LaCie/Midia/Series/"
set folderName to newbase as text
tell application "Finder"
if newbase contains "Season" then
if not (exists folder (POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text)) then
set p to path to "/Volumes/LaCie/Midia/Series/"
--make new folder at p with properties {name:newbase}
make new folder with properties {name:folderName, location:p}
else
move theFile to POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text
set name of result to newbase
end if
else
move theFile to POSIX file "/Volumes/LaCie/Midia/Filmes/"
end if
end tell
end hazelProcessFile
on Replace(input, x, y)
set text item delimiters to x
set ti to text items of input
set text item delimiters to y
ti as text
end Replace
The only part that doesn't work is the part which make the folder if it doesn't exists... Any ideas?

It would be easier to use a shell script, but try something like this:
on hazelProcessFile(theFile)
set text item delimiters to ":"
set filename to last text item of (theFile as text)
set text item delimiters to "."
if filename contains "." then
set base to text items 1 thru -2 of filename as text
set extension to "." & text item -1 of filename
else
set base to filename
set extension to ""
end if
set text item delimiters to {"720p", "HDTV", "x264", "IMMERSE", "-", "E01", "..."}
set ti to text items of base
set text item delimiters to ""
set newbase to ti as text
set newbase to replace(newbase, "S0", "Season ")
set newbase to replace(newbase, ".", " ") & extension
tell application "Finder"
if newbase contains "Season" then
move theFile to POSIX file "/Volumes/LaCie/Midia/Series/"
set name of result to newbase
else
move theFile to POSIX file "/Volumes/LaCie/Midia/Filmes/"
end if
end tell
end hazelProcessFile
on replace(input, x, y)
set text item delimiters to x
set ti to text items of input
set text item delimiters to y
ti as text
end replace
Embedded scripts can't contain handlers
External scripts have to use hazelProcessFile
theFile is an alias so you have to coerce it to text
You have to tell Finder to move files and convert paths to file objects with POSIX file
Restoring text item delimiters is not necessary as far as I know
Edit: here is a shell script version:
basename=${1##*/}
base=${basename%.*}
[[ $basename = *.* ]] && ext=".${basename##*.}" || ext=
for x in 720p HDTV x264 IMMERSE - E01 ...; do base=${base//"$x"}; done
base=${base//S0/Season}
base=${base//./ }
if [[ $base = *Season* ]]; then
mv "$1" "/Volumes/LaCie/Midia/Series/$base$ext"
else
mv "$1" /Volumes/LaCie/Midia/Movies/
fi
Edit 2: this would for example move here.comes.honey.boo.boo.s01e04.720p-killers.mkv to a folder named Here Comes Honey Boo Boo Season 1:
d=${1##*/}
if [[ $d =~ [Ss][0-9][0-9][Ee][0-9][0-9] ]]; then
d=$(sed -E 's/[ .][Ss]([0-9][0-9])[Ee][0-9][0-9].*/ Season \1/;s/ Season 0/ Season /;s/\./ /g' <<< "$d")
d=$(ruby -r rubygems -e 'require "titlecase"; puts ARGV[0].titlecase' "$d")
target="/Volumes/LaCie/Midia/Series/$d"
mkdir -p "$target"
mv "$1" "$target"
else
mv "$1" "/Volumes/LaCie/Midia/Movies"
fi
The titlecase gem can be installed with sudo gem install titlecase.

Related

Applescript, add to bottom of text file

I'm running a script for each item of a list,
each time it's adding a line to the top of a text file, but I actually need to change that order, so it write behind the last one (at the bottom)
here is my current script
et dataBackupFile to (path to desktop folder as text) & "Repport Data.txt"
insertOnTop from informationOnThisAccount into dataBackupFile
on insertOnTop from theData into theFile
try
set fileDescriptor to open for access file theFile with write permission
if (get eof fileDescriptor) > 0 then
set theContent to read fileDescriptor as «class utf8»
else
set theContent to ""
end if
set eof fileDescriptor to 0
write (theData & theContent) to fileDescriptor as «class utf8»
close access fileDescriptor
return true
on error
try
close access file theFile
end try
return false
end try
end insertOnTop
Replace
set eof fileDescriptor to 0
write (theData & theContent) to fileDescriptor as «class utf8»
with
write (theData & theContent) to fileDescriptor as «class utf8» starting at eof

AppleScript : add variable to text file

I running an AppleScript, which is saving the variable I want in a textfile, here is my code :
do shell script "echo " & quoted form of myDateTime & " >> Users/kevin/Documents/data/data_Backup.txt"
do shell script "echo " & quoted form of myNote & " >> Users/kevin/Documents/data/data_Backup.txt"
It's doing what I want, but the each time I run the script, the new data are added at the button of the text, and the oldest data are at on top, how can I have the newest data on top instead?
E.g
data_backup.txt data 3 data 2 data 1
also can I add a count on the text
E.g
data_backup.txt data3 case3, data2 case2, data1 Case 1
You need the read/write commands of AppleScript to read the text, insert the new data at the beginning and write it back.
This is a handler which returns true on success otherwise false
set dataBackupFile to (path to documents folder as text) & "Data:data_Backup.txt"
insertOnTop from "foo" into dataBackupFile
insertOnTop from "bar" into dataBackupFile
on insertOnTop from theData into theFile
try
set fileDescriptor to open for access file theFile with write permission
if (get eof fileDescriptor) > 0 then
set theContent to read fileDescriptor as «class utf8»
else
set theContent to ""
end if
set eof fileDescriptor to 0
write (theData & theContent) to fileDescriptor as «class utf8»
close access fileDescriptor
return true
on error
try
close access file theFile
end try
return false
end try
end insertOnTop

UNIX ZIP(1L) doesn't include top Directory of Application

I made, better I rummage together an AppleScript that results encrypted zip Files. It works except for Applications. If I expand the Archiv it is not an Application anymore. How can I compress valid Applications?
tell application "Finder"
set theItem to ((choose file) as alias)
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".zip")
display dialog "Enter password" default answer "" buttons {"OK"} default button 1
set thePassword to (text returned of result)
end tell
set cmd to "zip -P " & thePassword & " -rj " & zipFile & " " & itemPath & " -x *.DS_Store"
do shell script cmd
Check the -y option for zip. It may be what you need.
From iPhone: Compressing .app files in command line (Mac OS X) removes CodeSigning

TCL string toupper (each word)

I have a snippet of code that I use for a program that I have [Thus some of the app specific code] ...Anyway I am trying to capitalize the first letter of each word unless the word is in caps.
for example: >>this is text THAT would be CHANGED.
The code that i have thus far is as follows.
Again some of this is app specific I am not able to use "puts," the result has to be returned as return "" this is the reason that I create a var and add to it word by word.
proc ToTitle {} {
set Input [sh_set clipboard]
set CleanedInput [string map {" " |} [string trimright [string trimleft $Input]]]
set InputList [split $CleanedInput "|"]
set wresult ""
set item 0
foreach line $InputList {
set List_Item [lindex $InputList $item];
if {[string is upper $List_Item] == 1} {
set newline $List_Item
set wresult "$wresult $newline"
incr item
} else {
set newline [string totitle $List_Item]
set wresult "$wresult $newline"
incr item
}
}
regsub -all {\u0020{2,}} $wresult " " wresult; #REMOVE ALL EXCESSIVE SPACE CHARACTERS
set $wresult [string trimright [string trimleft $wresult]]; # TRIM ALL OF THE WHITESPACE BEFORE AND AFTER THE STRING
return "$wresult"}
This is currently working the output would be:
This Is Text THAT Would Be Changed.
The issue is the "Changed." because of the "."
The question is What can I use to only read the word character on items that have special characters or word characters?
{[string is upper $List_Item] == 1}
I know there is something that I can add to that to check it...
Thankyou in advance for all the help.
I think there's a simpler solution. Try this:
set a "this is text THAT would be CHANGED."
set out ""
foreach word $a {
append out "[string toupper $word 0 0] "
}
puts $out
Running it gives this output:
% % % This Is Text THAT Would Be CHANGED.

Self concatenate strings on csh

I need to concatenate partial content from argv to one of my variable.
I will show you my code:
#!/bin/csh
set stringList = ""
foreach param ($argv)
if($param !~ TEST) then
set stringList = $stringList " " $param
endif
end
echo $stringList > /tmp/prova.txt
Of course, nothing is printed on the txt file.
Any solution? Thanks.
Change
set stringList = $stringList " " $param
to
set stringList = "$stringList $param"

Resources