How do I make Applescript to compress 2 filez with the same name?

Hello guys and thank you for reading this!


I don't know a lot about programming, but i believe applescript can save my life. What I need is.


  1. Detecting 2 files with the same name, but differnt extension. (song 1.jpg, song 1.mp3)
  2. Compressing those 2 files into a single .zip (In the end I would have song 1.jpg, song 1.mp3 and song 1.zip


And that's it! I know its pretty simple, and I've seen the "Create Archive" in automator, but I don't really know how to tell him which files to Compress.


Thanks again!

Mac OS X (10.6.8)

Posted on Sep 18, 2012 6:13 PM

Reply
6 replies

Sep 20, 2012 7:34 AM in response to MarsComet

Hi,


Try this script :

----------------------------

setsourceFoldertochoose folder

tell application "System Events"

{name, name extension} of (filesofsourceFolderwhosename extensionisnot "zip")

mycompress2FilesSameName(theresult, sourceFolderasstring)

repeatwithiin (getfoldersofsourceFolder) --check in subfolders

{name, name extension} of (filesofiwhosename extensionisnot "zip")

mycompress2FilesSameName(theresult, pathofi)

endrepeat

endtell


oncompress2FilesSameName(tList, tFolder)

set tnames to item 1 of tList

settExtensionstoitem 2 oftList

set tc to count tnames

set j to 1

repeat while j < tc

tell (item j of tExtensions) to if it is not missing value or it is not "" then

set n1 to (count) + 2

else

set n1 to 1

end if

tell (item (j + 1) of tExtensions) to if it is not missing value or it is not "" then

set n2 to (count) + 2

else

set n2 to 1

end if

set name1 to item j of tnames

set name2 to item (j + 1) of tnames

if (text 1 thru -n1 of name1) = (text 1 thru -n2 of name2) then -- same name

set tFile1 to quoted form of POSIX path of (tFolder & name1)

set tFile2 to quoted form of POSIX path of (tFolder & name2)

set zipFile to quoted form of POSIX path of (tFolder & (text 1 thru -n1 of name1) & ".zip")

do shell script "/usr/bin/zip -j " & zipFile & " " & tFile1 & " " & tFile2

set j to j + 2

else

set j to j + 1

end if

endrepeat

endcompress2FilesSameName

----------------------------


if an archive already exists with the same name (in the parent folder of the files to compress) , the ZIP command overwrite this ".zip" file.


Example with "song 1.jpg", "song 1.mp3" and "song 1.zip" :

If "song 1.zip" doesn't exist, the ZIP command create the "song 1.zip" file.

If "song 1.zip" exist, the ZIP command overwrite the "song 1.zip" file (it will not create a new archive).

Sep 20, 2012 2:05 PM in response to MarsComet

MarsComet wrote:


I get the error "error "System Events got an error: AppleEvent timed out." number -1712", Is there a way to fix it,


Yes,


Replace the first part in the script by this

------------------------------

setsourceFoldertochoose folder

withtimeoutof 0 seconds

tell application "System Events"

{name, name extension} of (filesofsourceFolderwhosename extensionisnot "zip")

mycompress2FilesSameName(theresult, sourceFolderasstring)

repeat with i in (get folders of sourceFolder) --check in subfolders

{name, name extension} of (files of i whose name extension is not "zip")

my compress2FilesSameName(the result, path of i)

end repeat

end tell

endtimeout

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I make Applescript to compress 2 filez with the same name?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.