How to Duplicate an AppleScript function in Objective-C? - objective-c

This AppleScript code gives the name of files that have been dropped onto the script. How do I do the same in an Objective-C app? Would an application written in Objective-C be able to boot the JAR file using the file name as an argument to JAR?
on open of theFiles -- Executed when files are dropped on the script
set fileCount to (get count of items in theFiles)
repeat with thisFile from 1 to fileCount
set theFile to item thisFile of theFiles
set theFileAlias to theFile as alias
tell application "Finder"
set fileInfo to info for theFileAlias
set fileName to name of fileInfo
-- something to this effect, but now that you have the file name,
-- do what you will...
do shell script "cd /Desktop/RunJar/; java -jar " & fileName
end tell
end repeat
end open
We need to replace this AppleScript with a compiled app that can run a JAR that has been dropped onto the compiled app.

To get the path to the JAR file, your app must first implement Drag-and-Drop. See Drag and Drop Programing Topics for Cocoa (or as PDF)

As for actually running the jar:
do shell script "cd /Desktop/RunJar/; java -jar " & fileName
Use NSTask. The difference is that NSTask does not run a shell script; it runs the program (in this case, java) directly. You will need to set the task's working directory before running it.

Related

How to access a file within program file without the full path

I am using c++/winrt within a WinUI3 project and I am trying to have the application pull up a text file where users can select an option from it. The file I am trying to reach is in the project folder and included in the project. Currently, I have the file path set to my device so it pulls the text file from my directory. I want the application to be able to read from the program files instead of the long file path that is currently coded. I tried to change the relative path of the file and used the fstream function to read the file. I also tried just using "Aircrafts/Aircrafts.txt" but that did not work either.
Here is a snippet of the code.
fstream aircraftFile;
string info;
void MainWindow::loadPlatformData()
ifstream aircraftFile; //File Object for list
aircraftFile.open("C:\\Users\\TimmyK\\Documents\\GitHub\\sentinel3\\Sentinel3\\Aircrafts\\Aircrafts.txt", ios::in);
My guess is that you are writing a UWP project which is why the full-path access to your file fails. UWP's have restricted access to the file system per Microsoft Docs.
For a UWP project, you mark the file as Content Yes in the file properties so it's placed into your application's layout package. Normally the 'current working directory' is pointing to your packaged program's installed location. You can get a full path to this directory using:
auto installdir = Windows.ApplicationModel::Current().InstalledLocation();
std::wstring str = installdir.Path().c_str();
For Win32 "classic" desktop applications, there's no specific packaging or installation solution so there's lots of different ways to do it. In Visual Studio when you start the debugger or run a program, the "current working directory" is going to be the project directory but if you run the EXE from the command-line, it will be in a different directory.
To find the running EXE's directory per IInspectable, use GetModuleFileName:
wchar_t exePath[MAX_PATH] = {};
DWORD nc = GetModuleFileNameW(nullptr, exePath, MAX_PATH);
if (nc > MAX_PATH || nc == 0)
{
// Error condition
}
A typical pattern is to look in the EXE folder, then 'walk up' the directory to find asset files. This is what we do a lot in DirectX samples for Win32. See FindMedia.

A plugin to open a terminal in a particular directory selected from the project tree

I use git bash for my terminal configuration and it has --cd option where I can pass the path to open terminal in:
I have pretty deeply nested directory structure and thought it would be very convenient to open terminal in the directory I select in the tree:
Is there a plugin that can do that? If not, how hard will it be to write my own simple plugin?
It will probably be very basic:
read the currently selected folder path
run "open terminal" action supplied the command line string with the concatenated path
Thanks
there are no such plugins, please vote for IDEA-116724 and linked tickets.
You can try opening sh as External tool instead, like:
Program: C:\Windows\System32\cmd.exe
Parameters: /C "start sh.exe"
Working directory: $FileDir$
the only issue is that it will be opened as external window, not inside IDE

Run an exe file with dependencies VB.Net

I know there are plenty of questions about this topic but I reviewed them and I couldn't find what I need.
I need to run an exe in VB.Net but that exe needs some files that are in the exe's folder. When I try to run the exe using Shell or Process.Start() the exe looks for those files in my app folder (and throws an error) instead of the exe original folder. I can't move my app exe nor the external exe.
Have you tried Directory.SetCurrentDirectory Method ? It sets the application's current working directory to the specified directory. change the example below with the path of the application you want to execute prior calling it.
Imports System
Imports System.IO
Directory.SetCurrentDirectory("C:\test")
Console.WriteLine("Current directory: {0}", Directory.GetCurrentDirectory())
'Execute an application from C:\test
Output :
Current directory: C:\test
You might be able to use the Windows startup path:
exePath = System.Windows.Forms.Application.StartupPath

how to execute script without installing the exe of its program

I am executing a autoit script when autoit is installed in the system by using the following code
objProcess = New System.Diagnostics.Process()
objProcess.StartInfo.Arguments = "abc"
objProcess.StartInfo.FileName = "Z:\Scripts\test.au3"
objProcess.Start()
But I need to do same by giving a path to a unzipped files instead of installing it on system. Any idea how can I do this.
e.g My autoit path where I extracted files are z:\Software\autoit3.exe and my scripts path is "Z:\Scripts\test.au3".
You can compile your script files with the included Au2Exe compiler. This will turn your .au3 files into .exe files and they can be run without needing to install AutoIt on the computer first.
Use the command line parameters as described in the helpfile, and run the autoit3.exe as the main process.
At it's most basic you would use something like:
objProcess.StartInfo.FileName = "path\\autoit3.exe"
objProcess.StartInfo.Arguments = "\"path\\test.au3\" abc" // You can add any arguments for the script afterwards.
Note that I have never used vb.net, so don't expect anything I write to work first time :P But that is the basic idea.

how to execute a batch file using a relative path added to the project

i have created a batch file and have added it to the project using add items. Basically what i am aiming at is to execute this file on a button click action.
I am using System.Diagnostics.Process.Start("hello.bat") command to run this file
i have changed the build action to resource for this batch file.
But when i run this program, it is not able to locate the batch file.
I am required to give a relative path as the path my vary from machine to machine. how can i make this file accessable using a relative path?
Resource puts it inside of your EXE as data. You can google how to write a vb.net resource to a file, use the io tempfilename function to get a tempfile and use that (appending .bat), then run the batchfile from the name you gave it.
If you can ship the .bat with your EXE, this is convenient for debugging and production:
* Put the batchfile in the BIN subdir (debug or release) with your exe. May have to click 'show all files' in project explorer to see these dirs. Right click the .bat and pick 'include in project'. Don't make it a resource.
Run it using application.startuppath & "\" & batfilename. (application.startuppath is only in winforms. You can google 'how to get exe path in vb.net console app' etc. if you need another way).