Copy .app to applications folder

Mac OSX specific forum
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Copy .app to applications folder

Post by collectordave »

Driving me nuts.

Is it possible to copy a .app file into the applications folder?

and then run that application.

I have tried the following to copy but it fails!

Code: Select all

Sourcefile.s = GetUserDirectory(#PB_Directory_Documents) + "Temp/My New.app"

DestinationFile.s = GetUserDirectory(#PB_Directory_Programs) + "My New.app"

NSFM = CocoaMessage(0, 0, "NSFileManager defaultManager")


CocoaMessage(0, NSFM, "copyItemAtPath:$", @sourceFile, "toPath:$", @destinationFile, "error:", #nil)

I can do it with terminal commands

cd /Applications

cp -r /Users/MyName/Documents/Temp/MyNew.app MyNew.app

But using filberts AppleScript how do I combine the two commands?

I add Do Shell Script to each to no avail.

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Copy .app to applications folder

Post by wombats »

This works for me:

Code: Select all

CopyFile(GetCurrentDirectory() + "prog.app", "/Applications/prog.app")
The actual Applications folder seems to be /Applications, not /Username/Applications.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Copy .app to applications folder

Post by collectordave »

Unfortunately that copies the .app file but not the content of the file.

I am having some success doing a recursive copy of the content.

Just cannot set the icon for the file just yet.


Cheers

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Copy .app to applications folder

Post by wombats »

Oh, sorry. I should have tested it further.

Try RenameFile. It worked for me.

Code: Select all

RenameFile(GetCurrentDirectory() + "prog.app", "/Applications/prog.app")
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Copy .app to applications folder

Post by collectordave »

Lovely works out of the box.

Fails when source does not exist (In docs)

Also fails if destination exists.

But great.

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Copy .app to applications folder

Post by collectordave »

Having problems deleting the app.

DeleteFile and DeleteDirectory do not work

So I do the following

First I move the app from applications to a temporary folder and convert to a normal folder

DestinationaFile.s = "/Applications/MyNew.app"

Debug "Make Back File" ;Removes app from /Applications and converts to folder
RenameFile(DestinationaFile,GetUserDirectory(#PB_Directory_Documents) + "Temp/MyNew")

This works but DeleteDirectory() still does not work

MyFolder.s = GetUserDirectory(#PB_Directory_Documents) + "Temp/MyNew"

;Delete the actual executable
Debug DeleteFile(MyFolder + "/Contents/MacOS/MyNew", #PB_FileSystem_Force)

;Delete the MacOS folder
Debug DeleteDirectory(MyFolder + "/Contents/MacOS", "*.*", #PB_FileSystem_Recursive|#PB_FileSystem_Force)

;Delete the Contents folder
Debug DeleteDirectory(MyFolder + "/Contents", "*.*", #PB_FileSystem_Recursive|#PB_FileSystem_Force)

;Does not work on its own needs the above three statements
Debug DeleteDirectory(MyFolder,"")

This then deletes the whole thing.

I do use recursive and force etc.

I can only assume it is something to do with permissions on individual files in the folder?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Copy .app to applications folder

Post by Marc56us »

Regardless of the operating system, there are several conditions to be able to delete a directory.

1. Not to be there nor in one of its sub-directories (so under PB do a SetCurrentDirectory(<elsewhere> before)
2. That there are no files left open in any of these directories by any other program.
3. Have sufficient permissions (remember that in a unix-based system, you need +x permissions to browse a directory).

:wink:
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Copy .app to applications folder

Post by collectordave »

Hi Marc56us

1. Definitely in a different folder but tried setcurrentdirectory as well still the same.
2. No files in use by any other programme
3. Permissions checked the folder was created by the programme and can be deleted using finder no problem no passwords asked for.

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Copy .app to applications folder

Post by collectordave »

Hi All

Ok got the copy bit working but I am still deleting the original in the applications folder.

Copying using this code.

Code: Select all

 NSFM = CocoaMessage(0, 0, "NSFileManager defaultManager")
  destinationFile.s = "/Applications/World Stamp Collector(Mac).app"
  sourceFile.s = "/Volumes/World Stamp Collector/World Stamp Collector(Mac).app"

  CocoaMessage(0, NSFM, "copyItemAtPath:$", @sourceFile, "toPath:$", @destinationFile, "error:", #nil)
  
Is there an NSFileManager command to overwrite the original so I do not have to copy it out and then delete?

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Copy .app to applications folder

Post by collectordave »

Just a dim moment.

What I am trying to do is replace the application installed originally with an updated version downloaded from the internet.

Once changed replace to Remove and then copy new one I looked again.

Code: Select all

  NSFM = CocoaMessage(0, 0, "NSFileManager defaultManager")
  destinationFile.s = "/Applications/World Stamp Collector(Mac).app"
  sourceFile.s = "/Volumes/World Stamp Collector/World Stamp Collector(Mac).app"
  
  If ExamineDirectory(#PB_Any,destinationFile,"*.*") <> 0
    ;Delete Existing Application
    CocoaMessage(0, NSFM, "removeItemAtPath:$", @destinationFile, "error:", #nil)
    
  EndIf
  
  ;Now Copy The new application
  CocoaMessage(0, NSFM, "copyItemAtPath:$", @sourceFile, "toPath:$", @destinationFile, "error:", #nil)
  
I simply download the DMG file
mount it
run this then detach the dmg

Works quick and easy now.

Sorry to bother everyone.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply