ImageJ macro code to change directory - macros

I am learning how to write macros in ImageJ. I have the user select a folder where data is stored, e.g.,
path=getDirectory("Choose a data folder");
Once the user has selected the folder, e.g.,
path = D:\data_superfolder\data_folder
I then need to access a file that is up one level, e.g.,
newpath = D:\data_superfolder
In Matlab to move up one level all i need to do is,
cd('..')
which is super simple, but I've read through the ImageJ user manual I can't find similar code. How do I do this?
Thanks!

You can append /.. to a directory path to refer to its parent folder.
Here is an example macro that prompts the user to choose a directory, then lists the contents of its parent.
path = getDirectory("Choose a folder");
list = getFileList(path + "/..");
for (i = 0; i < list.length; i++) {
print(list[i]);
}

Ok, I had a similar problem, tried the fix suggested here and I couldn't get it to work... However, what worked for me was to define a new variable corresponding to the parent of your initial input directory, using the function File.getParent().
in example:
path = getDirectory("Choose a folder");
parent_path = File.getParent(path);
list = getFileList(parent_path);
for (i = 0; i < list.length; i++) {
print(list[i]);
}
so in your case, once the user has selected the folder, e.g.,
path = D:\data_superfolder\data_folder
the variable "parent_path" will be:
parent_path = D:\data_superfolder
Cheers!

Related

How to add custom code snippets using nodejs or other language in VSCode like webstorm?

In the webstorm I can do this using live-template using groovy to create custom template.
eg:
capitalize(camelCase(groovyScript( "def splitList = _1.split('/');def size = splitList.size();if (size >= 2) {for (int i = 0; i < size-2; i++) {splitList = Arrays.copyOfRange(splitList, 1, splitList.length);}};return splitList.join('_');", fileRelativePath() )));
This custom variable will create a string which is based on current directory and current filename and extentions.
If a directory like this
root
|-app
|-components
|MyComponent.tsx
|-server
MyComponent.tsx use this template will create a string which value is ComponentsMyComponentTsx
The reason create this string is i can command+o quick open by name which is found in the react-dev-tool.
I just copy the name and open de editor, command+o open the file. soconvenient
I want to know how can i do this in vscode

Why when materials gets instanced, using renderer.sharedMaterial is the new instance but not the file?

I have a list of materials set by hand called LookUpMaterials and an object which I want to compare if it has those materials.
If both materials correlate, add them to a new list "ChangeableMaterials".
I'm comparing using the sharedMaterials so everything is fine at first. But, when adding the .material to this new list, it creates a instance of the material.
Note: Creating the instance using .material is intended since I'm gonna edit this material anyway.
List<Material> ChangeableMaterials = new List<Material>();
Renderer[] renderers = GetComponentsInChildren<Renderer>();
for(int i = 0; i < renderers.Length; i++)
{
for(int j = 0; j < renderers[i].sharedMaterials.Length; j++)
{
if(LookUpMaterials.Contains(renderers[i].sharedMaterials[j]))
{
ChangeableMaterials.Add(renderers[i].materials[j];
}
}
}
In the first comparison (i,j == 0,0), it runs ok and the material is added to the list. All materials turns into Instances. But then when using sharedMaterials also spits out materials (Instances) in place of the original files and the comparison fails.
I've read multiple times that .sharedMaterials should be the file and not the instance, this is not being the case!
Can someone help me? I can't find the mistake here, just plain confused..
Here's 3 lists how Debug.Log() spits the name of the material
LookUpMaterials
011_Pillow
.materials
011_Pillow (Instance)
.sharedMaterials
011_Pillow (Instance) - Shouldnt be only 011_Pillow?

How do I find the full directory for an IFile in Cake?

I'm writing a script to produce some artefacts from my build so I want to clean up unwanted files first. I'm using CleanDirectory(dirPath, predicate).
I'm finding it disturbingly hard to work out the directory for a file. If I use GetDirectoryName() that seems to just get me the immediate parent, not the full directory path.
Func<IFileSystemInfo, bool> predicate =
fileSystemInfo => {
// Dont filter out any directories
if (fileSystemInfo is IDirectory)
return false;
var path = fileSystemInfo.Path.FullPath;
var directory = ((DirectoryPath)path).GetDirectoryName();
...
}
Obviously I can use the .NET Framework System.IO classes to do this easily but then I get strings with the slashes in the wrong direction, and things do not smoothly inter-operate with Cake which uses POSIX paths.
OK I've worked out a solution. The key to IFileSystemInfo is to try and cast the Path to various derived types/interfaces, which then provide the functionality you are probably looking for. Example:
Func<IFileSystemInfo, bool> predicate =
fileSystemInfo => {
// Dont filter out any directories
if (fileSystemInfo is IDirectory)
return false;
// We can try and cast Path as an FilePath as know it's not a directory
var file = (FilePath) fileSystemInfo.Path;
if (file.FullPath.EndsWith("Help.xml", StringComparison.OrdinalIgnoreCase))
return false;
// GetDirectory() returns a Path of type DirectoryPath
var directory = file.GetDirectory().FullPath;
...
}

Upload Servlet with custom file keys

I have built a Server that you can upload files to and download, using Eclipse, servlet and jsp, it's all very new to me. (more info).
Currently the upload system works with the file's name. I want to programmatically assign each file a random key. And with that key the user can download the file. That means saving the data in a config file or something like : test.txt(file) fdjrke432(filekey). And when the user inputs the filekey the servlet will pass the file for download.
I have tried using a random string generator and renameTo(), for this. But it doesn't work the first time, only when I upload the same file again does it work. And this system is flawed, the user will receive the file "fdjrke432" instead of test.txt, their content is the same but you can see the problem.
Any thoughts, suggestions or solutions for my problem?
Well Sebek, I'm glad you asked!! This is quite an interesting one, there is no MAGIC way to do this. The answer is indeed to rename the file you uploaded. But I suggest adding the random string before the name of the file; like : fdjrke432test.txt.
Try this:
filekey= RenameRandom();
File renamedUploadFile = new File(uploadFolder + File.separator+ filekey+ fileName);
item.write(renamedUploadFile);
//remember to give the user the filekey
with
public String RenameRandom()
{
final int LENGTH = 8;
StringBuffer sb = new StringBuffer();
for (int x = 0; x < LENGTH; x++)
{
sb.append((char)((int)(Math.random()*26)+97));
}
System.out.println(sb.toString());
return sb.toString();
}
To delete or download the file from the server you will need to locate it, the user will input the key, you just need to search the upload folder for a file that begins with that key:
filekey= request.getParameter("filekey");
File f = new File(getServletContext().getRealPath("") + File.separator+"data");
File[] matchingFiles = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(filekey);
}
});
String newfilename = matchingFiles[0].getName();
// now delete or download newfilename

How to copy the XPO files out of version control...partial code working, bizarre issue

I began upgrading our layers to Roll Up 7 while we still were developing in another environment with TFS turned on. We were at say version 1850, and by the time I finished, we were at 1900. So the goal is to merge in the 50 different check-ins into the completed RU7 environment. Each check-in can contain many different objects, and each object is stored in TFS as an XPO somewhere.
My code is 90% of the way there, but the issue arrises when copying the files out of the temp directory. When I look in the temp directory, the files aren't there, but somehow they're able to be accessed.
static void Job33(Args _args)
{
#File
SysVersionControlSystem sysVersionControlSystem = versioncontrol.parmSysVersionControlSystem();
SysVersionControlTmpItem contents;
SysVersionControlTmpChange change;
SysVersionControlTmpChange changes;
int i;
SysVersionControlTmpItem contentsAddition;
SysVersionControlTmpItem contentsItem;
str writePath;
Set permissionSet = new Set(Types::Class);
str fileName;
int n;
;
change = versioncontrol.getChangesHistory();
// BP deviation documented
changes.setTmp();
changes.checkRecord(false);
changes.setTmpData(change);
while select changes
order by changes.ChangeNumber asc
where changes.ChangeNumber > 1850
{
writePath = #'C:\TEMP\' + int2str(changes.ChangeNumber) + #'\';
contentsAddition = versioncontrol.getChangeNumberContents(changes.ChangeNumber);
n = 0;
while select contentsAddition
{
// HOW DOES THIS LINE ACCESS THE FILE BUT MY METHOD CAN NOT??
contentsAddition.viewFile();
//?????????????
// Write to appropriate directory
if(!WinAPI::pathExists(writePath))
WinAPI::createDirectory(writePath);
n++;
fileName = int2str(changes.ChangeNumber) + '_' + int2str(n) + '.xpo';
if (WinAPI::fileExists(contentsAddition.fileName(), false))
{
// Write to appropriate directory
if(!WinAPI::pathExists(writePath))
WinAPI::createDirectory(writePath);
WinAPI::copyFile(contentsAddition.fileName(), writePath + fileName, true);
info(strfmt("|%1|%2|", contentsAddition.fileName(), writePath + fileName));
}
}
info(strfmt("%1", changes.ChangeNumber));
}
}
Buried in Classes\SysVersionControlFilebasedBackEndTfs there is a .Net assembly that is used. I was able to use this to extract what I needed mixed in with the upper code. After I used this...my code from above started working strangely enough??
Somehow there was a file lock on the folder that I copied TO, that just wouldn't let me delete it until I closed AX...no big deal, but it suggests there is a tfsProxy.close() method or something I should have called.
Microsoft.Dynamics.Morphx.TeamFoundationServer.Proxy tfsProxy = new Microsoft.Dynamics.Morphx.TeamFoundationServer.Proxy();
;
tfsProxy.DownloadFile(contentsAddition.InternalFilename, changes.ChangeNumber, writePath + fileName);
So you are trying to just get the objects that were changed so you can import them into the new RU7 environment? Why not do this within Visual Studio directly? You can pull the XPOs from there based on the history of changesets since you started the RU7 upgrade.
Also, you should use branching for this. It would have been easy to just branch the new code in that way. Something you should look into for the future.