Windows batch to Mac Applescript

Afternoon

I have been trying to create a “batch like” file for a Mac, after doing a few searches I have discovered that this isn’t as straight forward as I had previously thought. My searchs have lead me to believe that using Applescript might be the best course of action however my knowledge of this is almost nothing and I am having some difficultly in getting started.

What my file needs to do is:

  1. Create a folder on the desktop with the folder structure

Desktop
-Folder 1
—Folder2 (sub folder of folder 1)
-----Folder3 (sub folder of folder 2)
-----Folder4 (sub folder of folder 2)

  1. Copy 3 files from the desktop that has been extracted from a .zip, to folder 4
  2. Go to the shared folder on the Mac and rename a file and then move it to folder 3
  3. Rename a file in folder 4 and the copy it to the shared folder
  4. Run an application.
  5. Preform a clean up by deleting Zip and Unzipped folders

(also the batch file I created for windows would check if installation was in programfiles or programfile(x86) which is not needed on Mac OS X i guess)

I have figured out how to create a folder on the desktop when stating the user name but the issue is the file will need to be universal to any users.

Also other than creating the Folder 1 I have just run into error.

Any advice would be great!

Cheers

(Please see below for windows version of file I created)


@echo on

CD %USERPROFILE%\desktop\

MD Folder-1

CD %USERPROFILE%\desktop\Folder-1

MD Folder-2

CD %USERPROFILE%\desktop\Folder 1\Folder-2

MD Folder-3

MD Folder-4

CD %USERPROFILE%\desktop\Folder-1\Folder-2\Folder-4\

COPY “%USERPROFILE%\Desktop\UnZippedFolder\TFF.xls”
COPY “%USERPROFILE%\Desktop\UnZippedFolder\Firm.hex”
COPY “%USERPROFILE%\Desktop\UnZippedFolder\instructions(hex).pdf”

IF EXIST "%PROGRAMFILES%\Company\Programmer" GOTO YES

GOTO NO

:yes
COPY fim.hex "%PROGRAMFILES%\Company\Programmer"
REN “%PROGRAMFILES%\Company\Programmer\old.hex” old2.hex
MOVE “%PROGRAMFILES%\Company\Programmer\old2.hex” "%USERPROFILE%\desktop\Folder-1\Folder-2\Folder-3"
REN “%PROGRAMFILES%\Company\Programmer\firm.hex” old.hex
CD “%PROGRAMFILES%\Company\Programmer*.*”
start Updater.exe

pause

CD %USERPROFILE%\desktop\

DEL “Zip”

RD /S /Q “%USERPROFILE%\desktop\Unzippedfolder”

exit

:no
COPY fim.hex "%PROGRAMFILES(x86)%\Company\Programmer"
REN “%PROGRAMFILES(x86)%\Company\Programmer\old.hex” old2.hex
MOVE “%PROGRAMFILES(x86)%\Company\Programmer\old2.hex” "%USERPROFILE%\desktop\Folder-1\Folder-2\Folder-3"
REN “%PROGRAMFILES(x86)%Company\Programmer\firm.hex” old.hex
CD “%PROGRAMFILES(x86)%\Company\Programmer*.*”
start Updater.exe

pause

CD %USERPROFILE%\desktop\

DEL “Zip”

RD /S /Q “%USERPROFILE%\desktop\Unzippedfolder”

exit

I’m on windows now in the library, so I’m not able to do any AppleScripting, but this sounds like an easy script to start with as a new scripter.

If you’re really interested in learning AppleScript, you can find some very good (free) tutorials here on MacScritpter.net.

Hope it helps,
ief2

Thank you, I will have a look and see what I can achieve after reading through these!

Thought I had made some progress earlier as Was able to create Folder 1 and Folder 2 but then could not create folder 3 and 4 so :frowning:

Cheers

You can use the statement “path to me” to get the path to the current users homefolder and use that for further reference.
All universally accessible applications resides in the folder /Applications.

The do shell script is a statement you want to learn fast, because then you can use unix commands for achieving your goals.

Be prepared to struggle a little bit with understanding the way you reference paths in AppleScript.
The posix path of is also an essential command for your goals.

The easy way would be for instance if you should copy a file from a shared disk to a users desktop to do something like this.

set sharedFile to “/Volumes/SharedDisk/theArchieve.zip”

set desktopFolder to POSIX path of (path to desktop)

set homeFolder to POSIX path of (path to “cusr” from user domain)

– lets say you wanted to copy an archieve to a newly created folder on the users desktop:
do shell script "mkdir " & desktopFolder & “NewFolder”
set newFolder to desktopFolder & NewFolder
– an important statement is the “log” statement, not sure if this turns out totally right.
do shell script "cp " & sharedFile & " " & newFolder
do shell script "cd " & newFolder & " ; unzip " & “theArchieve.zip”

Good Luck

McUsr

I hopes this helps you a little on your journey.

Thank you for your help guys!

McUsr - I didn’t really understand the usage of POSIX or do shell script so I avoided using it. However I will look to use this in future when I have more understanding of it.

So this is what I came up with in the end:



property firmwareVersions : {"v2-3-19", "v2-4-15"}

set installFolder to ((path to desktop) as string)
set productFolder to (((path to desktop) as string) & "Company:")
set product to ((((path to desktop) as string) & "Company:") & "Product")
set v2319 to (((((path to desktop) as string) & "Company:") & "Product:") & "v1:")
set v2415 to (((((path to desktop) as string) & "Company:") & "Product:") & "v2:")
set newFiles to (((path to desktop) as string) & "NewFirm:")
set sharedFolder to "Macintosh HD:Users:Shared:"
set oldFirm to "oldFirm.hex"
set oldfirmPath to (sharedFolder & oldFirm) as text
set newFirm to "newfirm.hex"
set newfirmPath to (v2415 & newFirm) as text
set rename to (sharedFolder & newFirm) as text
set zip to (((path to desktop) & "NewFirm.zip") as string)

tell application "Finder"
	
	make folder to installFolder with properties {name:"Company"}
	make folder at productFolder with properties {name:"Products"}
	repeat with i from 1 to (count firmwareVersions)
		make folder at product with properties {name:(item i of firmwareVersions)}
	end repeat
	
	duplicate (files of folder newFiles) to v2415
	
	move (oldfirmPath as alias) to (v2319 as alias) with replacing
	duplicate (newfirmPath as alias) to (sharedFolder)
	set name of file (rename as alias) to (oldFirm)
	
	launch application "FirmwareUpdater"
	
	delete file zip
	delete folder newFiles
	
end tell

What you think?

It is probably messy and formatted wrong but if you provide me with some feedback I will improve on this!

Cheers

It is actually not bad. Only the setup of all the variables can be done cleaner. I think you use to much path to desktop. If you want to change the installation directory, you’ll have to change all the occurrences of (path to desktop).

Using the variable installFolder, which is set in the beginning, for all other setups of the variables, seems a better idea to me.

The part were the Finder comes in isn’t bad. Although I don’t understand why you use make folder to instead of make folder at sometimes.


property firmwareVersions : {"v2-3-19", "v2-4-15"}

set installFolder to path to desktop folder as text
set productFolder to (installFolder & "Company:") as text
set product to (productFolder & "Product:") as text

set v2319 to (product & "v1:") as text
set v2415 to (product & "v2:") as text

set newFiles to (installFolder & "NewFirm:") as text
set sharedFolder to "Macintosh HD:Users:Shared:"

set oldFirm to "oldFirm.hex"
set oldfirmPath to (sharedFolder & oldFirm) as text

set newFirm to "newfirm.hex"
set newfirmPath to (v2415 & newFirm) as text
set rename to (sharedFolder & newFirm) as text

set zip to (installFolder & "NewFirm.zip") as text

tell application "Finder"
	
	make folder to installFolder with properties {name:"Company"}
	make folder at productFolder with properties {name:"Products"}
	repeat with i from 1 to (count firmwareVersions)
		make folder at product with properties {name:(item i of firmwareVersions)}
	end repeat
	
	duplicate (files of folder newFiles) to v2415
	
	move (oldfirmPath as alias) to (v2319 as alias) with replacing
	duplicate (newfirmPath as alias) to (sharedFolder)
	set name of file (rename as alias) to (oldFirm)
	
	launch application "FirmwareUpdater"
	
	delete file zip
	delete folder newFiles
end tell

Good job,
ief2

PS: You could of course leave any interference with the Finder out of this with some do shell script-ing.

Thank you :slight_smile:

I will bear this in mind in future.

In what instance would you use at instead of to?

Also I noticed you changed the variables from as string to as text, why was this? To be honest I am not to sure why I wrote as string in the first place, it just worked so I ran with it.

I had attempted to use the variable names but I ran into a few errors which is why I did it the way I have.

As I have it working now I will refine it so it looks more like your script.

Thank you again!

Hello

You did really well without the do shell script! :slight_smile:

Best Regards

McUsr

Finder Dictionary

Still I have no idea about the difference. But as it seems that the at-parameter is required I suggest using it.

Actually I’ve got this from StefanK. The path to + constant returns an Alias. If you add as text, it coerces it to a string, which does not necessarily puts a ‘:’ at the end. The as text, instead, doesn’t coerce the alias. It’s a property of the alias. You can be sure there is a ‘:’ at the end.

I hope it helps,
ief2