Is there a way to specify in the build script whether you want to use the file-based or directory-based format for an IntelliJ project?

You can specify whether you want to use the file-based or directory-based format when importing a Gradle project into Intellij. However if you use the idea task in the Gradle gui it always uses the file-based format.

Is there a way to specify in the build script which format you want to use?

The ‘idea’ task only supports the file-based format.